template class typecast question
I have a question regardin a smart pointer teamplate class Im writing.
I have named the class SPoint and I have run into a problem in the following case:
I have a baseclass class Base
and a child class class Child:public Base
I have a function that used to take a pointer to the base class (and therefor couled be used even with a pointer to the child class), and now I want it to take a smart pointer SPoint<Base> The problem I run into is that now I no longer can call thefunction with a smart pointer SPoint<Child>
Is there someway to do that typecast from SPoint<Chiled> to SPoint<Base>?
The SPoint template class only have two pointers as data members so it is not a problem tecnically but I want to know how to write it so the compiler understand.
Another way to solve it is to put the Child pointer in a SPoint<Base> smartpointer but then I have to manually typecast the pointer at every time I want to use a function from the Chiled class (and to me thats nor really a smart pointer but a rather dumb one)
Hopfully there are someone out there that understands my question, I tried to explain as clear as I couled
Even though Base and Child are related, SPoint<Base> and SPoint<Child> are not. If you want a conversion to occur, you will have to provide the appropriate (templated?) constructor or conversion operator yourself in the class.
I'd suggest you have a look at Boost's smart pointer classes, since they do implement such behaviour. You might learn a trick or two from their source code.
I'd suggest you have a look at Boost's smart pointer classes, since they do implement such behaviour. You might learn a trick or two from their source code.
thanx for the quick answer!
Yes I know that SPoint<Base> and SPoint<child> are not releted that is the problem.
I was looking for the syntax to implement a constructor in the SPoint class that couled handle the conversion.
I will google on "Boost's smart pointer" and see what I can come up with.
Yes I know that SPoint<Base> and SPoint<child> are not releted that is the problem.
I was looking for the syntax to implement a constructor in the SPoint class that couled handle the conversion.
I will google on "Boost's smart pointer" and see what I can come up with.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement