C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

confusion involving pointer to pointer when converting from

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Kavya
Guest





PostPosted: Sat Dec 23, 2006 10:10 am    Post subject: confusion involving pointer to pointer when converting from Reply with quote



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?
Back to top
Gianni Mariani
Guest





PostPosted: Sat Dec 23, 2006 10:11 am    Post subject: Re: confusion involving pointer to pointer when converting f Reply with quote



Kavya wrote:
Quote:
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.

Quote:

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.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.