C ++:创建一个共享对象,而不是一个指向一个对象的共享指针 [英] C++: Creating a shared object rather than a shared pointer to an object
问题描述
boost :: shared_ptr真的打扰我。当然,我理解这样的东西的效用,但我希望我可以使用 shared_ptr< A>
作为 A *
。考虑下面的代码
boost::shared_ptr really bothers me. Certainly, I understand the utility of such a thing, but I wish that I could use the shared_ptr<A>
as an A*
. Consider the following code
class A
{
public:
A() {}
A(int x) {mX = x;}
virtual void setX(int x) {mX = x;}
virtual int getX() const {return mX;}
private:
int mX;
};
class HelpfulContainer
{
public:
//Don't worry, I'll manager the memory from here.
void eventHorizon(A*& a)
{
cout << "It's too late to save it now!" << endl;
delete a;
a = NULL;
}
};
int main()
{
HelpfulContainer helpfulContainer;
A* a1 = new A(1);
A* a2 = new A(*a1);
cout << "*a1 = " << *a1 << endl;
cout << "*a2 = " << *a2 << endl;
a2->setX(2);
cout << "*a1 = " << *a1 << endl;
cout << "*a2 = " << *a2 << endl;
cout << "Demonstrated here a2 is not connected to a1." << endl;
//hey, I wonder what this event horizon function is.
helpfulContainer.eventHorizon(a1);
cout << "*a1 = " << *a1 << endl;//Bad things happen when running this line.
}
创建有帮助的容器的人没有想到别人想要保留指针到A对象。我们不能给HelpClass boost :: shared_ptr对象。但我们可以做的一件事是使用pimlp成语创建一个SharedA,它本身是一个A:
Whoever created the HelpfulContainer wasn't thinking about others wanting to retain pointers to A objects. We can't give HelpfulClass boost::shared_ptr objects. But one thing we could do is use the pimlp idiom to create a SharedA which itself is an A:
class SharedA : public A
{
public:
SharedA(A* a) : mImpl(a){}
virtual void setX(int x) {mImpl->setX(x);}
virtual int getX() const {return mImpl->getX();}
private:
boost::shared_ptr<A> mImpl;
};
然后main函数可以看起来像这样:
And then the main function can look something like this:
int main()
{
HelpfulContainer helpfulContainer;
A* sa1 = new SharedA(new A(1));
A* sa2 = new SharedA(sa1);
cout << "*sa1 = " << *sa1 << endl;
cout << "*sa2 = " << *sa2 << endl;
sa2->setX(2);
cout << "*sa1 = " << *sa1 << endl;
cout << "*sa2 = " << *sa2 << endl;
cout << "this demonstrates that sa2 is a shared version of sa1" << endl;
helpfulContainer.eventHorizon(sa1);
sa2->setX(3);
//cout << "*sa1 = " << *sa1 << endl;//Bad things would happen here
cout << "*sa2 = " << *sa2 << endl;
//but this line indicates that the originally created A is still safe and intact.
//only when we call sa2 goes out of scope will the A be deleted.
}
因此,我的问题是:以上模式一个好的模式,还是有一些我还没有考虑的事情。我当前的项目继承了一个有用的容器
类,删除了我需要的指针,但我仍然需要在有限的容器中的数据结构。
So, my question is this: Is the above pattern a good pattern, or is there something I'm not considering yet. My current project inherited a HelpfulContainer
class like above that's deleting the pointers that I need, but I still need the data structure present in the HelpfulContainer.
Update: This question is a follow-on question.
推荐答案
我认为对于一个对象关心自己的生命周期(有一些例外)的设计通常是 。如果一个对象可以做一个工作,并且其他的东西管理它的创建和销毁,选择最简单的生命周期策略(例如局部/自动变量),这通常是更有用的。
I think that it is usually poor design for an object to care about its own lifetime (there are exceptions). It is usually more useful if an object can do a job and something else manages its creation and descruction, choosing the simplest lifetime strategy possible (e.g. local/automatic variable).
如果你绝对必须在两个不合作的东西之间共享所有权(例如 shared_ptr
和 AssistanceContainer
)那么你将不得不使用某种代理技术。
If you absolutely have to share ownership between two things that don't co-operate (such as shared_ptr
and HelpfulContainer
) then you will have to use some sort of proxy technique.
在这种情况下,它只是看起来像有用的容器
对你的情况没有帮助。
In this case, though, it just looks like HelpfulContainer
just isn't that helpful for your situation.
这篇关于C ++:创建一个共享对象,而不是一个指向一个对象的共享指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!