[Insight-users] Inheritance problem with SmartPointer

Luis Ibanez luis.ibanez at kitware.com
Tue, 27 Apr 2004 08:35:34 -0400


Hi Yann,


       Your compiler is right.


You may have:

    Class B deriving from class A,

but

    SmartPointer< B >  *does not* derives from SmartPointer< A >


Actually, SmartPointer do not derive from any class.


Therefore, there is no reason why the function
in the base class should be suitable for accepting
SmartPointers of the derived class.

The solution is quite simple:

    You should pass only raw pointers as arguments.

the raw pointer of B will be accepted as a valid
raw pointer to A.

If you want to manage reference counting, then the trick
is to recieve such raw pointers in SmartPointer as soon
as you start the function.  Something like:


void SetToUnion( const A * a, const B * b )
{
   SmartPointer< A >  sa = a;
   SmartPointer< B >  sb = b;

   sa->CallMeAMethod();
   sb->InvokeAnotherMethod();

}

However the only motation for doing this is if you
think that the reference counting of the classes
pointed by "a" and "b" may change while you are
still running this method.  In practice, this is
seldom the case.



Regards,



    Luis


---------------------
Lemeur Yann wrote:

> Hi everybody,
> Here is my problem: I have a class B which inherit from class A. In this class A there is a public method called 
> virtual void SetToUnion(A::Pointer x, A::Pointer y) 
> where Pointer is defined like this: typedef SmartPointer<A> Pointer.
> 
> I don't redefine the SetToUnion method in class B.
> In class B there is another typedef : typedef SmartPointer<B> Pointer;
> I've made a test program for class B where I call SetToUnion like this:
> 
> 
> B::Pointer x = B::New();
> B::Pointer y = B::New();
> ......
> .....
> B::Pointer z = B::New();
> z->SetToUnion(x,y);       <--------------------- Daesn't compile!
> 
> There is an error at compilation which tells me something like this : 
> 
> "No matching fonction for call to B::SetToUnion(B::Pointer, B::Pointer). Candidates are A::SetToUnion(A::Pointer,A::Pointer)"
> 
> I'm sure there is no error in the test program. To my mind, the compiler crashes because it daesn't understand that B::Pointer is also a A::Pointer (by inheritance)...
> Daes somebedy know a solution?
> Thanks a lot!
> 
> Yann
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users
>