如何实现引用构造函数?
我正在寻找一种方法让我的构造函数以这种方式引用另一个对象:
Foo object1("File1");
Foo object2("File1");
第一个对象正常创建。第二个对象发现已经有一个对象正在使用“File1”参数,并使其自身成为对第一个对象的引用。我知道以这种方式可能无法直接做到这一点。我保留 Foo* 的静态向量来跟踪分配的对象。
我知道我可以创建类指针的所有成员,并在第一种情况下创建(新建)它们。在第二种情况下,我不会创建它们并将它们指向第一个对象。此外,第二个对象的生命周期也保证比第一个对象短。
那么,有没有一种简单/优雅的方法来做到这一点?
编辑:感谢您提供的所有出色的解决方案。我都喜欢,但只能用其中之一。我将保留所有这些知识以供将来参考。
我选择static map
解决方案。起初我认为这很愚蠢,但经过进一步审查,我发现它很优雅。
我现在能看到的唯一补充就是向 FooObject 添加一个链接计数器。这样,在 Foo 的构造函数中,我可以增加链接计数器。在析构函数中,递减计数器。如果计数器为零,则将其从地图中删除。这样就不会出现内存泄漏,并且对象可以按任何顺序被破坏。我猜这种方法是shared_ptr esque。 这要归功于@Travis。
谢谢,詹姆斯。
I am looking for a way to have my constructor make a reference to another object in this way:
Foo object1("File1");
Foo object2("File1");
The first object is created normally. The second object sees that there is already an object that is using the "File1" parameter and makes itself a reference to the first object. I know this may not be directly possible to do this in this manner. I keep a static vector of Foo* to keep track of the allocated objects.
I know that I can make all the members of the class pointers and for the first case create (New) them. In the second case, I would not create them and point them at the first object. Also the second object is guaranteed to have a shorter lifetime than the first.
So, is there an easy/elegant way to do this?
EDIT: Thank you for all the great solutions. I like them all, but I can only use one. I will keep all of this knowledge for future reference tho.
I am selecting the static map <string, FooObject*>
solution. At first I thought it was stupid, but upon further review, it appealed to me as elegant.
The only addition that I can see right now would be to add a link counter to the FooObject. This way in the constructor of Foo I can increment the link counter. In the destructor, decrement the counter. If counter is then zero, remove it from the map. This way there is no memory leaks, and the objects can get destructed in any order. I guess this methodology is shared_ptr esque.
Credit goes to @Travis for this.
Thanks, James.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用
static map
作为变量,其中 object 是您需要的类的其余部分。Try to use
static map<string, object*>
as variable where object is the rest of the class you need.您可以使用资源管理器。它应该有一个像这样的接口:
在它内部将搜索是否有 Foo* 对象使用与新 id 相同的 id,如果存在,它将返回旧的 Foo* 对象,否则它将创建一个 Foo* 类型的新对象。它还会计算每个 id 的 Foo* 对象的数量。当调用 Release 函数时,它将减少该 id 的编号(以唯一的方式标识对象),如果它为 0,则将删除该对象。
这是最简单的方法。您可以让它在另一个线程中运行以获取不止一种类型的资源和其他内容,但这是基本思想。
You can use a resource manager. It should have an interface like this:
Inside it will search if there is Foo* object that uses same id like the new id and if this exits it will return old Foo* object else it will create a new object of type Foo*. It will also count the number of Foo* objects for every id. When Release function is called it will decrement the number for that id(that identifies in a unique the object) and if it's 0 it will delete the object.
This is the most simple way of doing this. You can have it running in another thread for more that one type of resource and other stuff, but this is the basic idea.
这听起来可能像 Flyweight http://en.wikipedia.org/wiki/Flyweight_pattern
如果你在单线程环境中,您可以在类中拥有一个静态
std::map
并由构造函数使用。您可以使用 pimpl 模式来存储实际的实现,并在创建对象时检查该实现是否已经可用。如果是,请立即设置指针。如果您需要在没有剩余引用时进行清理,您也可以添加其他代码来处理该问题。This sounds possibly like flyweight http://en.wikipedia.org/wiki/Flyweight_pattern
If you're in a single-threaded environment you could just have a static
std::map<std::string, ObjectImpl*>
in your class and used by your constructor. You would use the pimpl pattern to store the actual implementation and when an object is created check to see if the implementation is already available. If it is, just set the pointer immediately. If you need to clean up when there are no references left, you can add additional code to handle that as well.您只需要一个返回 Foos 的工厂方法,并始终调用它并期望引用。
然后,您将它们存储在静态地图中,并在系统询问时检查它们。
You just need a factory method that returns Foos and always call it expecting a reference.
Then you store them in a static map and you check for them when you are asked.