Quick template newbie question

D

Dilip

Hi

This is more of a "is-this-really-the-right-way-to-do-this" style of
question.

I have a class hierarchy resembling:

class base
{
};

template<typename T>
class derv : public base
{
};


// elsewhere...
std::vector<base*> baseptrs;
baseptrs.push_back(new derv<int>());

Now if I want to downcast from base to derv -- is the syntax for that:

derv<int>* pObj = dynamic_cast<derv<int>* >(baseptrs.at(0));

I am a little unsure. if I end up having more than 2 template
parameters in a class, doesn't this kind of syntax quickly get hairy
(ignoring the fact that I may have to rethink the use of dynamic_cast
anyway)
 
V

Victor Bazarov

Dilip said:
Hi

This is more of a "is-this-really-the-right-way-to-do-this" style of
question.

I have a class hierarchy resembling:

class base
{
};

template<typename T>
class derv : public base
{
};


// elsewhere...
std::vector<base*> baseptrs;
baseptrs.push_back(new derv<int>());

Bad idea. Your 'base' doesn't seem to declare its d-tor virtual.
Now if I want to downcast from base to derv -- is the syntax for that:

derv<int>* pObj = dynamic_cast<derv<int>* >(baseptrs.at(0));

I am a little unsure. if I end up having more than 2 template
parameters in a class, doesn't this kind of syntax quickly get hairy
(ignoring the fact that I may have to rethink the use of dynamic_cast
anyway)

Yes, and yes.

Why do you think you need to dynamic_cast?

V
 
D

Dilip

Victor said:
Bad idea. Your 'base' doesn't seem to declare its d-tor virtual.

I know -- that was just an example.
Yes, and yes.

Why do you think you need to dynamic_cast?

One particular member function in that polymorphic bunch (for some
reason or the other) is available only in the derived class
(non-virtual). I have to downcast to be able to call it.
 
F

Fei Liu

Dilip said:
I know -- that was just an example.


One particular member function in that polymorphic bunch (for some
reason or the other) is available only in the derived class
(non-virtual). I have to downcast to be able to call it.

why not define an empty base virtual function holder or a public
non-virtual forwarding function to be able to polymorphically use your
class hierarchy? And if your base has no virtual function (you should
at least have virtual ~base()), you can't use dynamic_cast.
 
D

Dilip

Fei said:
Dilip wrote:

why not define an empty base virtual function holder or a public
non-virtual forwarding function to be able to polymorphically use your
class hierarchy? And if your base has no virtual function (you should
at least have virtual ~base()), you can't use dynamic_cast.

I worked around it by removing the need for that non-virtual function
altogether. Now I feel better :)

And yes I have other virtual functions in the base class, yes I know
about the forwarding trick, yes my base class' ctor is virtual and yes
I know dynamic_cast cannot be used if you don't have a polymorphic
hierarchy . This was a simple problem so I didn't want to complicate
it by adding more code.
 
F

Fei Liu

Dilip said:
I worked around it by removing the need for that non-virtual function
altogether. Now I feel better :)

And yes I have other virtual functions in the base class, yes I know
about the forwarding trick, yes my base class' ctor is virtual and yes
I know dynamic_cast cannot be used if you don't have a polymorphic
hierarchy . This was a simple problem so I didn't want to complicate
it by adding more code.

I thought you were asking a 'quick template newbie question'? :)
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top