validity of using subclasses in pointer-to-pointer types?

Discussion in 'C++' started by Marcus Alanen, Sep 7, 2003.

  1. Hello everybody, I'm having a bit of trouble with the following code.
    I have a class A, and its subclass B. Then, a variable "a" of type A**,
    and one "b" of type B*.

    class A {
    };
    class B : public A {
    };

    A** a;
    B* b;

    int main() {
    a = &b;
    }


    For some reason I can't assign "a = &b". I've tested it with g++ version
    3.3 and Intel's C compiler (icc version 7.0) and both give similar
    errors:

    G++:
    test.cpp: In function `int main()':
    test.cpp:14: invalid conversion from `B**' to `A**'

    ICC:
    test.cpp(14): warning #556: a value of type "B **" cannot be assigned to
    an entity of type "A **"
    a = &b;

    So it certainly sounds like this is not valid C++, but I can't help to
    wonder why not? "a" is supposed to be a double indirection to an object
    of type A, and since B is a subclass of type A, I would've thought this
    would work. In fact, saying "a = &static_cast<A*>(b)" compiles with g++,
    whereas icc says:

    test.cpp(14): error: expression must be an lvalue or a function designator
    a = &static_cast<A*>(b);


    On a final note, using "A* a" and "B b" works nicely on both compilers,
    so why shouldn't "A** a" and "B* b"?

    Any comments would be much appreciated. It's not a problem I can't do
    some workaround for, but I'd like to extend my knowledge of C++ here.
    Thanks in advance for any feedback!

    Regards,

    Marcus
    Marcus Alanen, Sep 7, 2003
    #1
    1. Advertising

  2. On Sun, 07 Sep 2003 13:38:53 +0300, Marcus Alanen <> wrote:

    >Hello everybody, I'm having a bit of trouble with the following code.
    >I have a class A, and its subclass B. Then, a variable "a" of type A**,
    >and one "b" of type B*.
    >
    >class A {
    >};
    >class B : public A {
    >};
    >
    >A** a;
    >B* b;
    >
    >int main() {
    > a = &b;
    >}
    >
    >
    >For some reason I can't assign "a = &b". I've tested it with g++ version
    >3.3 and Intel's C compiler (icc version 7.0) and both give similar
    >errors:
    >
    >G++:
    >test.cpp: In function `int main()':
    >test.cpp:14: invalid conversion from `B**' to `A**'
    >
    >ICC:
    >test.cpp(14): warning #556: a value of type "B **" cannot be assigned to
    >an entity of type "A **"
    > a = &b;
    >
    >So it certainly sounds like this is not valid C++, but I can't help to
    >wonder why not?



    Consider the effect of allowing the following code:


    #include <iostream>

    struct A {};
    struct B: A { int x; B(): x(1234) {}; virtual int f(){ return x; } };

    int main()
    {
    A a;
    B b;
    B* p = &b;
    B** pp = &p;
    A** pFudge = &p; // Not allowed, but suppose it is?

    *pFudge = &a; // Changes value of 'p'.
    std::cout << p->f() << std::endl; // <-- Bang, crash.
    }


    Essentially the same problem as with T const**, which you'll find in
    the FAQ.
    Alf P. Steinbach, Sep 7, 2003
    #2
    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. tuko
    Replies:
    4
    Views:
    369
    Dave Rahardja
    Sep 17, 2004
  2. jacob navia

    Pointer validity

    jacob navia, Dec 1, 2003, in forum: C Programming
    Replies:
    16
    Views:
    642
    Richard Bos
    Dec 4, 2003
  3. Ulrich Hobelmann
    Replies:
    9
    Views:
    394
    Ulrich Hobelmann
    Jul 3, 2006
  4. Replies:
    11
    Views:
    1,386
    James Kanze
    Jun 7, 2007
  5. Alfredo Di Napoli
    Replies:
    4
    Views:
    378
    Alfredo Di Napoli
    Feb 19, 2012
Loading...

Share This Page