Salve a tutti,
come posso fare in modo di sostituire i puntatori tradizionali con gli shared_ptr in un codice di questo tipo ?
Se provo a sostituire banalmente le righecodice:#include <iostream> #include <tr1/memory> #include <list> using namespace std; using namespace std::tr1; class Base; class Derived; typedef Base* spBase_t; typedef Derived* spDerived_t; class Base { public: int type; }; class Derived : public Base { public: void print() { cout << "Hello from Derived" << endl ; } }; int main() { list<spBase_t> l; //list of ponters to Base object spBase_t d1( new Derived() ); //create derived objects spBase_t d2( new Derived() ); //create derived objects l.push_back(d2); l.push_back(d2); for (list<spBase_t>::iterator i = l.begin(); i!= l.end(); i++) { ( (spDerived_t)(*i) )->print(); //current->print(); } }
concodice:typedef Base* spBase_t; typedef Derived* spDerived_t;
il compilatore (gcc 4.2.) restituisce un errore del tipo:codice:typedef shared_ptr<Base> spBase_t; typedef shared_ptr<Derived> spDerived_t;
/usr/include/c++/4.2.1/tr1/boost_shared_ptr.h:562: error: invalid conversion from ‘Base* const’ to ‘Derived*’
Grazie!

Rispondi quotando
