Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > confusion involving pointer to pointer when converting from derived to base

Reply
Thread Tools

confusion involving pointer to pointer when converting from derived to base

 
 
Kavya
Guest
Posts: n/a
 
      12-23-2006
Since Circle is-a Shape we are allowed to do this

Circle *c = new Circle;
Shape *s = c; //Works

But we can't do this

Circle **cc = &c;
Shape **ss = cc; //Does not works

Similarly, we can convert a pointer to non-const to a pointer to const
but we may not convert a pointer to pointer to non-const to a pointer
to pointer to const:

char *s1 = 0;
const char *s2 = s1; // works.
char *a[MAX];
const char **ps = a; // Does not works

Can someone explain this?

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      12-23-2006
Kavya wrote:
> Since Circle is-a Shape we are allowed to do this
>
> Circle *c = new Circle;
> Shape *s = c; //Works
>
> But we can't do this
>
> Circle **cc = &c;
> Shape **ss = cc; //Does not works


If this were to work, you would be able to do this:

Circle *c = new Circle;
Tri *t = new Tri;

Circle **cc = &c;
Shape **ss = cc;
* ss = t;

c.radius = 22; // OOOPS - undefined behaviour

which is not very desirable.

>
> Similarly, we can convert a pointer to non-const to a pointer to const
> but we may not convert a pointer to pointer to non-const to a pointer
> to pointer to const:
>
> char *s1 = 0;
> const char *s2 = s1; // works.
> char *a[MAX];
> const char **ps = a; // Does not works
>
> Can someone explain this?


Same thing.

char *s1 = 0;
const char foo[] = "FOO";
const char **ps = &s1; // is not allowed
*ps = foo; // s1 now points to foo
s1[0]='B'; // assigning a const char - not good.


 
Reply With Quote
 
 
 
 
BobR
Guest
Posts: n/a
 
      12-23-2006

Kavya wrote in message...
>Since Circle is-a Shape we are allowed to do this
>
>Circle *c = new Circle;
>Shape *s = c; //Works
>
>But we can't do this
>
>Circle **cc = &c;
>Shape **ss = cc; //Does not works
>
>Similarly, we can convert a pointer to non-const to a pointer to const
>but we may not convert a pointer to pointer to non-const to a pointer
>to pointer to const:
>
>char *s1 = 0;
>const char *s2 = s1; // works.
>char *a[MAX];
>const char **ps = a; // Does not works
>
>Can someone explain this?


Yep!

Alf P. Steinbach's "Pointers" document:
http://home.no.net/dubjai/win32cpptu...ters/ch_01.pdf

Read that, then come back if you need more help.

--
Bob R
POVrookie


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting derived class pointer to private base class pointer subramanian100in@yahoo.com, India C++ 8 08-18-2010 10:54 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) developereo@hotmail.com C++ 4 05-23-2007 09:32 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
[RTTI] cast base class pointer to <templated> derived class pointer tirath C++ 3 10-12-2003 01:44 PM



Advertisments