Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Quick template newbie question

Reply
Thread Tools

Quick template newbie question

 
 
Dilip
Guest
Posts: n/a
 
      04-21-2006
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)

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-21-2006
Dilip wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Dilip
Guest
Posts: n/a
 
      04-21-2006
Victor Bazarov wrote:
> Dilip wrote:
> > 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.


I know -- that was just an example.

> > 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?


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.

 
Reply With Quote
 
Fei Liu
Guest
Posts: n/a
 
      04-21-2006

Dilip wrote:
> Victor Bazarov wrote:
> > Dilip wrote:
> > > 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.

>
> I know -- that was just an example.
>
> > > 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?

>
> 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.

 
Reply With Quote
 
Dilip
Guest
Posts: n/a
 
      04-21-2006
Fei Liu wrote:
> Dilip wrote:


> > 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.


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.

 
Reply With Quote
 
Fei Liu
Guest
Posts: n/a
 
      04-21-2006

Dilip wrote:
> Fei Liu wrote:
> > Dilip wrote:

>
> > > 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.

>
> 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'?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick question, hopefully quick answer. ~misfit~ NZ Computing 114 01-06-2005 01:36 PM
Quick Question Quick Answer JKop C++ 11 05-24-2004 09:46 PM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments