防止临时件延长其使用寿命?
这也许是不可能的,但我想知道是否有可能使暂时的事物不再永远过去其最初的表达方式。我有一个指向父对象的对象链,以及一个将创建子对象的成员函数,这里是一个简化的示例
class person{
string name;
person * mommy;
public:
person(const string & nam, person * m = 0) : name(nam), mommy(m) {}
person baby(const string & nam){
return person(nam, this);
}
void talk() const{
if (mommy) mommy->talk();
cout << name << endl;
}
};
int main(){
person("Ann").baby("Susan").baby("Wendy").talk(); // fine
const person & babygirl = person("Julie").baby("Laura"); // not fine
babygirl.talk(); // segfault
return 0;
}
我想要使用 person
的方式是将其传递给一个函数,和这样的东西:
void use(const person & p) {
p.talk();
}
use(person("Anna").baby("Lisa"));
很好。
只要没有一个临时变量在原始表达式之后存活,这种方法就可以正常工作,但是如果我将最终的临时变量之一绑定到 const 引用,它的父级就不会存活,并且会出现段错误。我可以隐藏 person
的复制构造函数和赋值运算符,但是有什么方法可以防止这种错误的发生吗?如果可能的话,我想避免动态分配。
This may be impossible, but I was wondering if it was possible to keep a temporary from ever lasting past its original expression. I have a chain of objects which point to parent objects, and a member function which will create a child object, a simplified example is here
class person{
string name;
person * mommy;
public:
person(const string & nam, person * m = 0) : name(nam), mommy(m) {}
person baby(const string & nam){
return person(nam, this);
}
void talk() const{
if (mommy) mommy->talk();
cout << name << endl;
}
};
int main(){
person("Ann").baby("Susan").baby("Wendy").talk(); // fine
const person & babygirl = person("Julie").baby("Laura"); // not fine
babygirl.talk(); // segfault
return 0;
}
The way I want to use person
is to pass it to a function, and something like this:
void use(const person & p) {
p.talk();
}
use(person("Anna").baby("Lisa"));
Is fine.
This will work fine as long as none of the temporaries survive past the original expression, but if I bind one of the final temporaries to a const reference, its parents don't survive, and I get a segfault. I can hide person
's copy constructor and assignment operator, but is there any way I can prevent this kind of error from happening? I'd like to avoid dynamic allocation if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您正在这里创建一个数据结构,其中子级有指向其父级的指针。在这种情况下,使用临时工具肯定会给您带来痛苦。为了确保安全,您将需要动态分配并可能使用某种引用计数。
您是否考虑过使用
boost::shared_ptr
?它是引用计数智能指针类的实现。使用shared_ptr
以及一些工厂方法,您也许能够获得您想要的效果并减少动态内存分配的痛苦。我尝试了一下,似乎有效。一旦代码退出作用域,所有对象都会被销毁,因为没有对shared_ptr 的引用。编辑:
为了响应 zounds 的评论,我修改了示例,以便根对象控制数据结构的生命周期。
It looks like you are creating a data structure here where children have pointers to their parents. Using temporaries is guaranteed to cause you grief in this case. In order to make this safe, you will need to dynamically allocate and possibly use some sort of reference counting.
Have you considered using
boost::shared_ptr
? It is an implementation of a reference counted smart pointer class. Usingshared_ptr
and perhaps some factory methods, you might be able to get the effect you want and reduce the pain of dynamic memory allocation. I tried it out and it seems to work. Once the code exits the scope, the objects are all destroyed because there are no references left to the shared_ptrs.Edit:
In response to zounds' comment, I have modified the example so that the root object controls the data structure's lifetime.
你必须做这样的事情:
这样,父“a”不会超出范围,直到你真正完成它(在调用“use”之后)。
原始代码的问题在于,父级“Anna”只需停留足够长的时间来调用“baby”,并且可以在进行函数调用之前丢弃父级。通过将父级设置为具有作用域的变量,您可以控制它何时被破坏。
这对我来说看起来危险吗?是的。因此,我建议查看 m-sharp 关于动态分配的答案。但是如果你想要一个不需要引用计数等的方法,那么你可以这样做......
You'll have to do something like this:
That way, the parent "a" doesn't go out of scope until you're truly done with it (after the call to "use").
The problem with the original code is that the parent "Anna" only has to stay around long enough to call "baby" and the parent can be discarded before making the function call. By making the parent a variable with scope, you can control when it's destructed.
Does this look dangerous to me? Yes. Therefore I'd suggest a look at m-sharp's answer on dynamic allocation. But if you wanting a method that doesn't need reference counting, etc. then you can do it this way...