pointers-to-pointers and class children

P

Peteroid

I'm creating managed classes in an managed application, and believe I've
seen the following generate a compile error:

__gc class Parent_Class {} ;
__gc class Child_Class : public Parent_Class {} ;

__gc class Other_Class
{
public:
void Method_1( Parent_Class** pc_ptr_ptr ) {}

void Method_2( )
{
Child_Class* child ;

Method_1( &child ) ; // error
}
} ;

The error is something like " can't convert Child_Class __gc* __gc* to
Parent_Class __gc* __gc* ".

This seems odd. Why can't it convert to pointer-to-a-pointer of a child to a
pointer-to-a-pointer of its parent class?

[==Peteroid==]

PS - its possible this is one of those "there is a bug somewhere else"
cases, but thought i'd write anyway....hehe
 
C

Carl Daniel [VC++ MVP]

Peteroid said:
This seems odd. Why can't it convert to pointer-to-a-pointer of a
child to a pointer-to-a-pointer of its parent class?

This is standard C++. Child* -> Base* is OK, Child** to Base** is not OK

This is simply a matter of consistency with the C++ type system - a pointer
is not a class, so there is no inheritance relationship between two pointer
types (and hence no implicit conversion in either direction). Base** is no
more a "parent" to Child** than float** is to int**.

-cd
 
P

Peteroid

Thanks, Carl!

So I'm not crazy, that is the way it is. Just wanted to be sure it wasn't
something else dumb I might have been doing...hehe

[==Peteroid==]
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top