Arrays of smart pointers to base class

Discussion in 'C++' started by Rune Allnor, Sep 11, 2008.

  1. Rune Allnor

    Rune Allnor Guest

    Hi all.

    I am sure this is an oldie, but I can't find a useful suggestion on
    how to solve it.

    I have a class hierarchy of classes derived from a base class.
    I would like to set up a vector of smart pointers to the base
    class, and access the virtual functions of any class in the
    hierarchy through the virtual functions.

    For some reason the compiler doesn't like the way I do things
    (code showing intentions of use below).

    Any pointers to standard solutions are highly appreciated.

    Rune

    /////////////////////////////////////////////////////////////////////////////////////////
    #include <boost/scoped_ptr.hpp>
    #include <iostream>
    #include <vector>

    class base{
    public:
    virtual void report() const { std::cout << "Base Class Report" <<
    std::endl;};
    virtual ~base(){std::cout << "Base Class Destructor" << std::endl;};
    };

    class derivedA : public base{
    virtual void report() const{std::cout << "Derived A Class Report" <<
    std::endl;};
    virtual ~derivedA(){std::cout << "Derived A Class Destructor" <<
    std::endl;};
    };

    class derivedB : public base{
    virtual void report() const{std::cout << "Derived B Class Report" <<
    std::endl;};
    virtual ~derivedB(){std::cout << "Derived B Class Destructor" <<
    std::endl;};
    };

    int main(int argc,char* argv[])
    {
    std::vector<boost::scoped_ptr<base> > v;
    v.push_back(boost::scoped_ptr<base>(new base)); // Compile error
    v.push_back(boost::scoped_ptr<base>(new derivedA)); // Compile error
    v.push_back(boost::scoped_ptr<base>(new derivedB)); // Compile error

    std::vector<boost::scoped_ptr<base> >::iterator i;
    for (i=v.begin();i!=v.end();++i)
    (*i)->report();

    return 0;
    }
     
    Rune Allnor, Sep 11, 2008
    #1
    1. Advertising

  2. Rune Allnor

    Guest

    On 11 Sep, 15:21, Rune Allnor <> wrote:
    > Hi all.
    >
    > I am sure this is an oldie, but I can't find a useful suggestion on
    > how to solve it.
    >
    > I have a class hierarchy of classes derived from a base class.
    > I would like to set up a vector of smart pointers to the base
    > class, and access the virtual functions of any class in the
    > hierarchy through the virtual functions.
    >
    > For some reason the compiler doesn't like the way I do things
    > (code showing intentions of use below).
    >
    > Any pointers to standard solutions are highly appreciated.
    >
    > Rune
    >
    > ///////////////////////////////////////////////////////////////////////////­//////////////
    > #include <boost/scoped_ptr.hpp>
    > #include <iostream>
    > #include <vector>
    >
    > class base{
    > public:
    >         virtual void report() const { std::cout << "Base Class Report" <<
    > std::endl;};
    >         virtual ~base(){std::cout << "Base Class Destructor" << std::endl;};
    >
    > };
    >
    > class derivedA : public base{
    >         virtual void report() const{std::cout << "Derived A Class Report" <<
    > std::endl;};
    >         virtual ~derivedA(){std::cout << "Derived A Class Destructor" <<
    > std::endl;};
    >
    > };
    >
    > class derivedB : public base{
    >         virtual void report() const{std::cout << "Derived B Class Report" <<
    > std::endl;};
    >         virtual ~derivedB(){std::cout << "Derived B Class Destructor" <<
    > std::endl;};
    >
    > };
    >
    > int main(int argc,char* argv[])
    > {
    >         std::vector<boost::scoped_ptr<base> > v;
    >         v.push_back(boost::scoped_ptr<base>(new base)); // Compile error
    >         v.push_back(boost::scoped_ptr<base>(new derivedA)); // Compile error
    >         v.push_back(boost::scoped_ptr<base>(new derivedB)); // Compile error
    >
    >         std::vector<boost::scoped_ptr<base> >::iterator i;
    >         for (i=v.begin();i!=v.end();++i)
    >                 (*i)->report();
    >
    >         return 0;
    >
    >
    >
    > }- Hide quoted text -
    >
    > - Show quoted text -


    The following reworked code works:

    ///////////////////////////////////////////////////////////////////////////­//////////////
    #include <boost/shared_ptr.hpp>
    #include <iostream>
    #include <vector>


    class base{
    public:
    virtual void report() const { std::cout << "Base Class Report"
    <<
    std::endl;};
    virtual ~base(){std::cout << "Base Class Destructor" <<
    std::endl;};



    };


    class derivedA : public base{
    public:
    virtual void report() const{std::cout << "Derived A Class
    Report" <<
    std::endl;};
    virtual ~derivedA(){std::cout << "Derived A Class Destructor"
    <<
    std::endl;};


    };


    class derivedB : public base{
    public:
    virtual void report() const{std::cout << "Derived B Class
    Report" <<
    std::endl;};
    virtual ~derivedB(){std::cout << "Derived B Class Destructor"
    <<
    std::endl;};


    };


    int main(int argc,char* argv[])
    {
    std::vector<boost::shared_ptr<base> > v;
    v.push_back(boost::shared_ptr<base>(new base)); // Compile
    error
    v.push_back(boost::shared_ptr<base>(new derivedA)); // Compile
    error
    v.push_back(boost::shared_ptr<base>(new derivedB)); // Compile
    error

    std::vector<boost::shared_ptr<base> >::iterator i;
    for (i=v.begin();i!=v.end();++i)
    (*i)->report();


    return 0;


    }


    I changed the scoped_ptrs to shared_ptrs. scoped_ptr AFAIK is designed
    for simple RAII. I also declared the methods in derivedA and derivedB
    public (having private destructors didn't help!!).
     
    , Sep 11, 2008
    #2
    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. MotoK
    Replies:
    59
    Views:
    1,848
    Keith Thompson
    Sep 15, 2006
  2. Alf P. Steinbach
    Replies:
    6
    Views:
    557
    John Carson
    Sep 3, 2005
  3. n2xssvv g02gfr12930

    Smart pointers and member function pointers

    n2xssvv g02gfr12930, Nov 26, 2005, in forum: C++
    Replies:
    3
    Views:
    480
    n2xssvv g02gfr12930
    Nov 27, 2005
  4. Hicham Mouline
    Replies:
    1
    Views:
    601
    Victor Bazarov
    Apr 20, 2009
  5. Karan Rajput
    Replies:
    2
    Views:
    150
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page