Imagine having a vector of shared pointers
typedef vector< shared_ptr< classA > > PointerVector;
And a Class B which has as a member a vector of shared pointers as well, and a method that pushes back in this vector an already dereferenced shared Pointer.
ClassB
{
public:
void add(classA &ref);
private:
PointerVector vector;
}
Assume that main feeds the add function with a dereferenced shared_ptr< classA > instance:
in main:
shared_ptr< classA > sharedptr ( new classA );
ClassA &ref = *sharedptr;
ClassB B;
B.add(ref);
Is there a way to implement the add function to accept a dereferenced shared pointer and convert it to the initial shared pointer? and then push it back in the matrix?
This is what i am trying to do:
void ClassB::add(classA &ref) {
shared_ptr< classA > ptr = &ref;
vector.push_back( ptr );
}
NOTE1: When the classA is added to the vector, i want the shared pointer count increased by one. I dont need to create another shared pointer, i want to "find" the shared pointer the object previously had.
NOTE2: i cannot use make_shared because i am currently in tr1