By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,018 Members | 885 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,018 IT Pros & Developers. It's quick & easy.

Arrays of smart pointers to base class

P: n/a
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;
}
Sep 11 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 11 Sep, 15:21, Rune Allnor <all...@tele.ntnu.nowrote:
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 ClassReport" <<
std::endl;};
* * * * virtual ~derivedA(){std::cout << "Derived A Class Destructor" <<
std::endl;};

};

class derivedB : public base{
* * * * virtual void report() const{std::cout << "Derived B ClassReport" <<
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 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.