R
Rune Allnor
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;
}
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;
}