Template classes and containers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Prakash Bande

    #1

    Template classes and containers

    Hi,

    I have a template class as below:
    template <class _T> class myclass{
    public:
    myclass(){ obj = new _T();}
    _T* obj;
    _T* operator->() (return obj;}
    };

    I need to keep all the myclass objects in a STL container such as
    vector. But since every instantiation is a different type I cannot
    have a vector or myclass.
    So I derived myclass from a base class as below:
    class base{
    public:
    base(){}
    ~base(){}
    };
    template class<_T> myclass : public base

    Now I have a vector of base objects.
    The problem is that I want to call -> operator. I cannot make it pure
    virtual in base since the return type depends on template resolution.
    I cannot typecast base pointer since I do not know type because of
    template. For example:
    f()
    {
    base *a = new myclass<somecla ss>
    f1(a);
    }
    void f1(base *a)
    {
    // I want to do this here.
    a->callmethod() ; // Would have worked if -> could return
    myclass<somecla ss>*
    }

    Any suggestions?

    Thanks,
    Prakash
  • Alf P. Steinbach

    #2
    Re: Template classes and containers

    * Prakash Bande:[color=blue]
    >
    > I have a template class as below:
    > template <class _T> class myclass{[/color]

    Underscore followed by uppercase letter is reserved for the
    implementation.
    [color=blue]
    > public:
    > myclass(){ obj = new _T();}
    > _T* obj;[/color]

    This data member should not be public.
    [color=blue]
    > _T* operator->() (return obj;}
    > };[/color]

    [color=blue]
    >
    > I need to keep all the myclass objects in a STL container such as
    > vector. But since every instantiation is a different type I cannot
    > have a vector or myclass.
    > So I derived myclass from a base class as below:
    > class base{
    > public:
    > base(){}
    > ~base(){}
    > };
    > template class<_T> myclass : public base[/color]

    STL or standard library container requires among other things that your
    objects can be safely copied -- can they?


    [color=blue]
    > Now I have a vector of base objects.
    > The problem is that I want to call -> operator. I cannot make it pure
    > virtual in base since the return type depends on template resolution.
    > I cannot typecast base pointer since I do not know type because of
    > template. For example:
    > f()
    > {
    > base *a = new myclass<somecla ss>
    > f1(a);
    > }
    > void f1(base *a)
    > {
    > // I want to do this here.
    > a->callmethod() ; // Would have worked if -> could return
    > myclass<somecla ss>*
    > }[/color]

    Try to explain why you "need" to have these objects in a single container,
    or why you "need" to have them of different types.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    Working...