Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   validity of using subclasses in pointer-to-pointer types? (http://www.velocityreviews.com/forums/t277427-validity-of-using-subclasses-in-pointer-to-pointer-types.html)

Marcus Alanen 09-07-2003 10:38 AM

validity of using subclasses in pointer-to-pointer types?
 
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


Alf P. Steinbach 09-07-2003 10:42 AM

Re: validity of using subclasses in pointer-to-pointer types?
 
On Sun, 07 Sep 2003 13:38:53 +0300, Marcus Alanen <marcus.alanen@abo.fi> 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.



All times are GMT. The time now is 01:26 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.