"catching" a base class pointer as the derived class

F

FP

I have a function that returns a base class pointer as in:

baseClass<T>* FunctionA( );

As you can see the baseClass is templated. FunctionA is found in a
third templated class.

What I'm trying to do is on the other side of things "catch" the
returned value as the derived class. My call looks liked this:

object->derivedPointer = FunctionA( );

The derived class is also templated with <typename T>

I've tried using the following casts with no luck:


static_cast<basePtr>(object->derivedPtr) = FunctionA( );
dynamic_cast<basePtr>(object->derivedPtr) = FunctionA( );
(basePtr)(object->derivedPtr) = FunctionA( );

I'm using .Net 2003

Any help or an online referenc to look at would be appreciated.
 
J

John Harrison

FP said:
I have a function that returns a base class pointer as in:

baseClass<T>* FunctionA( );

As you can see the baseClass is templated. FunctionA is found in a
third templated class.

What I'm trying to do is on the other side of things "catch" the
returned value as the derived class. My call looks liked this:

object->derivedPointer = FunctionA( );

The derived class is also templated with <typename T>

I've tried using the following casts with no luck:


static_cast<basePtr>(object->derivedPtr) = FunctionA( );
dynamic_cast<basePtr>(object->derivedPtr) = FunctionA( );
(basePtr)(object->derivedPtr) = FunctionA( );

I'm using .Net 2003

You explaination isn't very clear, its usually better to post code than try
to describe code. However the following works for me, it's my best attempt
at reproducing your situation from your description.

template <class T>
class Base
{
};

template <class T>
class Derived: public Base<T>
{
};

template <class T>
class X
{
public:
void func()
{
Derived<T>* ptr = static_cast<Derived<T>*>(functionA());
}
Base<T>* functionA()
{
return new Derived<T>();
}
};

int main()
{
X<int> x;
x.func();
}

john
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top