[Insight-users] Inheritance problem with SmartPointer

Mathias Seitel mathias.seitel at igd.fhg.de
Tue, 27 Apr 2004 17:35:30 +0200


Hi Yann, Luis,

the boost library's smart pointer boost::shared_ptr allows for this kind 
of implicit upcast, by implementing copy constructors for arbitrary 
shared_ptr template instantiations. If your classes don't depend on the 
ITK class hierarchy, you could give boost a try (www.boost.org).

I wonder why ITK's smart pointer doesn't provide this functionality?

Mathias


Luis Ibanez wrote:

> 
> 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
>>
> 
> 
> 
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users
> 

-- 
Dipl.-Inf. (FH) Mathias Seitel
Fraunhofer Gesellschaft         Phone: +49-6151-155 484
Institute for Computer Graphics Fax: +49-6151-155 559
Fraunhoferstr. 5                mailto: mathias.seitel at igd.fhg.de
D-64283 Darmstadt, Germany      www: http://www.igd.fhg.de/igd-a7