lundi 26 janvier 2015

Constructor taking shared_ptr

I have situation like this



struct Foo
{
Foo(int x, int y) : x(x), y(y)
{
}
int x, y;
};

class Bar
{
public:
typedef std::shared_ptr<const Foo> ConstFooPtr;
typedef std::shared_ptr<Foo> FooPtr;

Bar(int index = 0, FooPtr ptr = FooPtr()) : index_(index), ptr_(ptr)
{
}

private:
ConstFooPtr ptr_;
int index_;
};


I want to produce Bar and 2 methods comes to my mind



Bar::FooPtr ptr(new Foo(1, 2)); //1

auto ptr2 = std::make_shared<Bar::FooPtr::element_type>(42, 13); //2

auto bar = Bar(0, ptr);


The first one is preety general, because if I will change the type of FooPtr perhaps I will not have to chage this code. But it uses new which is bad I guess.


Second doesn't use new, but it assumes that it is shared_ptr which is not general also.


Is there any method to make it work and be general? Or maybe I should never take some ptrs in constructor?


(I store ptr to const Foo because I will make copies of Bar and change index_, but data in ptr_ will be the same - You can asume that Foo is something big with some containers)


Aucun commentaire:

Enregistrer un commentaire