Hello all,
I have not tested the code I'm posting here to see if it compiles, but I have an issue in a large codebase and I believe that this should be the simplest program to demonstrate the issue;
class base {
public:
shared_ptr<base> clone()=0;
};
typedef shared_ptr<base> baseptr;
class derived : public base {
public:
baseptr clone(){
return baseptr(new derived);//the equiv of this compiles in my codebase,
//so it should work here
}
};
typedef shared_ptr<derived> derivedptr;
typedef vector<derivedptr> derivedList;
int main(){
derivedptr der(new derived);//equivelent comiles
derivedList list;//fine
list.push_back(der);//works
baseptr bas = der->clone();//works
derivedptr der2 = der->clone();//compiler error
//error C2440: 'initializing' : cannot convert from
//'std::tr1::shared_ptr<_Ty>' to
//'std::tr1::shared_ptr<_Ty>'
derivedptr der3 = (shared_ptr<derivedptr>) der->clone();//same as last one but with 'type cast'
list.push_back(der->clone()); //comiler error
//error C2664: 'void std::vector<_Ty>::push_back(_Ty &&)' :
//cannot convert parameter 1 from 'std::tr1::shared_ptr<_Ty>'
//to 'parse::AssemblyPtr[READ DERIVEDPTR] &&'
}
using std::shared_ptr and std::vector.
So my question is how do I convert from a shared_ptr<base> to shared_ptr<derived>?