pointer

Hi to all
can u tell me wt is the difference between them plz tell with some example i have lots of confusion abt it.
"const Fred* p", "Fred* const p" and "const Fred* const p",where fred is a class name.plz tell me with some example how they differ programmaticaly.
To quote out of one of Scott Meyers books:
1
2
3
4
char *p              = "Hello"  // non-const pointer, non-const data
const char *p        = "Hello"; // non-const pointer, const data
char * const p       = "Hello"; // const pointer non-const data
const char * const p = "Hello"; // const pointer, const data 

I have this explanation I wrote a long time ago stored away in a file, so to elaborate:

This file explains how const works.

The following declarations are identical:
const char* p;
char const* p;

Both declare a pointer to a constant character. The second is slightly
better in the sense that the declaration can be read from right-to-left:
"p is a pointer to a const char". Read as such, it is easy to see that
the line *p = 'c'; will not compile.

The following declaration:
char* const p;

declares p to be a constant pointer to a character. That is:
p = "foo"; // Does not compile
*p = 'f'; // Compiles!

And thus:
const char* const p;
char const* const p;

both declare p to be a constant pointer to a constant character, and
so none of the following lines of code compile:
p = "foo";
*p = 'f';


Now throw another pointer into the mix:
const char** p;
char const** p;

These are equivalent and declare p to be a pointer to a pointer to a
constant character. That is:
p = ptr-to-ptr-to-char; // Compiles
*p = ptr-to-char; // Compiles
**p = 'f'; // Does not compile

Or how about creative placement of const:
char* const* p;

This declares p to be a pointer to a constant pointer to a character.
That is:
p = ptr-to-constptr-to-char; // Compiles
*p = ptr-to-char; // Does not compile
*p = constptr-to-char; // Does not compile
**p = 'f'; // Compiles

And the ever-popular:
char** const p;

Which declares p to be a constant pointer to a pointer to a character.
Or:
p = ptr-to-ptr-to-char; // Does not compile
p = constptr-to-ptr-to-char; // Does not compile
*p = ptr-to-char; // Compiles
**p = 'f'; // Compiles

And now we get just plain const happy:
const char* const* p;

p is a pointer to a constant pointer to a constant character. The only
thing you can do with this one (besides remove the code and rewrite) is:
p = ptr-to-constptr-to-constchar;

const char** const p;
p is a constant pointer to a pointer to a constant character. The only
thing you can do with this is:
*p = ptr-to-constchar;

And this beast:
const char* const* const p;

Well, it won't pass code review since nobody will understand it, but at
any rate... We've achieved maximum constant-ness with this line. You
can't do anything at all with p, what it points to, what that points to,
or what "what that" points to. You can print it. That's about it.
DEar jsmith
But i have doubt when we use in object suppose we have a class
name fred so how these are different ,,,,,,,so how i use when we have const Fred* p", "Fred* const p" and "const Fred* const p here fred is a class name.AS CLASS CONATIN FUN AND DATA MEMBER so how to use these pointer,,,,,,,plz tell with some example,,,,
What is the advantage if we use virtual mechanism,what the advantage will get client if we use virtual mechanism.
Here is quick help to understand the virtual mechanizm in general C++ terms.

In other words, it is polymorphism. It is multiple forms or multiple of shapes of a
single thing, or a single interface for multiple forms of interaction.
This can be achieved by function overloading, operator overloading etc etc in C++.

For example, if you want to define a class with a bare basic functionality and allow its
derived classes to customize or override that basic functionality, you provide
a single interface/function to handle those varying definitions in run-time without too much
hectic code.
That is the time you would want to design such class with virtual functionality.

Example, a base Flight class which defines a bare basic functionality for flying speed,
engine capacity, altitude etc. These characteristics/functions are varied by the engine
capacity or flight type, such as derived classes, helicopter, a propeller engine, and jet engine.
You would define these varying fucntionalities within each derived flight class overriding
the base class provided bare/basic functionality.
And you want to provide a single functional interface to handle all types flights,checking
the object being processed and its overriding functionality.

As you may have already knew that a base class type pointer can recognize/identify its derived
child class down to the leaf level of hierarchy. Hence a base class type pointer parameter
in a function signature and handles to check/process the varying functionality of children,
would do a right job.

Quick example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void interface(Flight *pFlight)
{
   if (dynamic_cast<Helicopter *>(pFlight)) // returns null if not 
   {
         // .. handle the helicopter functionality
   }
   else if (dynamic_cast<Propeller *>(pFlight)) // returns null if not 
   {
         // .. handle the propeller-operated flight functionality
   }
   if (dynamic_cast<Jet *>(pFlight)) // returns null if not 
   {
         // .. handle the jet-operated flight functionality
   }
}

int main()
{
   // here you provide an interface to the client 
   
   Flight *myFlight;
   
   Helicopter helicopter;   //for client-1
   interface( &helicopter );   

   Propeller propeller;     // for client-2
   interface( &propeller );

   Jet jet;
   interface( &jet );  // for client-3 

    // other common functionality

    // all done   
}  


Here you provided one single interface for all type objects of a single family.
The dynamic_cast<> helps you to identify which child object is exactly pointed-to and what
functionality to be taken care of. This is done in run-time dynamically.

Hope this gives some idea of virtual or polymorphic functionality provided in C++.
For more details, you could check reference searching on "polymorphism".

Good luck :)



Topic archived. No new replies allowed.