By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,133 Members | 1,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,133 IT Pros & Developers. It's quick & easy.

confusion involving pointer to pointer when converting from derived to base

P: n/a
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?

Dec 23 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.
Dec 23 '06 #2

P: n/a

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
Dec 23 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.