M
Marcus Alanen
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
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