Quick template newbie question

Discussion in 'C++' started by Dilip, Apr 21, 2006.

  1. Dilip

    Dilip Guest

    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)
     
    Dilip, Apr 21, 2006
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Apr 21, 2006
    #2
    1. Advertising

  3. Dilip

    Dilip Guest

    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.
     
    Dilip, Apr 21, 2006
    #3
  4. Dilip

    Fei Liu Guest

    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.
     
    Fei Liu, Apr 21, 2006
    #4
  5. Dilip

    Dilip Guest

    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.
     
    Dilip, Apr 21, 2006
    #5
  6. Dilip

    Fei Liu Guest

    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'? :)
     
    Fei Liu, Apr 21, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wizard
    Replies:
    2
    Views:
    370
    Saravana
    Jan 3, 2005
  2. =?Utf-8?B?UnVkeQ==?=

    Quick Newbie Question

    =?Utf-8?B?UnVkeQ==?=, May 17, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    554
    Scott M.
    May 18, 2005
  3. Chris Theis
    Replies:
    2
    Views:
    494
    Chris Theis
    Jul 24, 2003
  4. tom_usenet
    Replies:
    0
    Views:
    559
    tom_usenet
    Jul 24, 2003
  5. JKop
    Replies:
    11
    Views:
    920
Loading...

Share This Page