confusion involving pointer to pointer when converting from derived to base

Discussion in 'C++' started by Kavya, Dec 23, 2006.

  1. Kavya

    Kavya Guest

    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?
    Kavya, Dec 23, 2006
    #1
    1. Advertising

  2. Re: confusion involving pointer to pointer when converting from derivedto base

    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.
    Gianni Mariani, Dec 23, 2006
    #2
    1. Advertising

  3. Kavya

    BobR Guest

    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/win32cpptut/special/pointers/ch_01.pdf

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

    --
    Bob R
    POVrookie
    BobR, Dec 23, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tirath
    Replies:
    3
    Views:
    696
    Ivan Vecerina
    Oct 12, 2003
  2. Replies:
    4
    Views:
    390
    Alf P. Steinbach
    May 23, 2007
  3. Replies:
    1
    Views:
    387
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    374
    Victor Bazarov
    May 23, 2007
  5. , India
    Replies:
    8
    Views:
    938
    gwowen
    Aug 18, 2010
Loading...

Share This Page