This question already has an answer here: What is The Rule of Three? 8 answers When you do this foo=creature(choice); a temporary creature object is created on the RHS of the assignment. Its destructor is called once the statement is done, ie at end of the line. There isn't really anything to fix, but you can initialize foo after reading in choice , rather than default initializing
这个问题在这里已经有了答案: 什么是三项规则? 8个答案 当你这样做 foo=creature(choice); 在作业的RHS上创建临时creature对象。 它的析构函数在声明完成后即被调用,即在行尾。 没有什么可修复的,但是你可以在choice阅读之后初始化foo ,而不是默认初始化,然后分配: int choice; cout<<"enter 1 2 or 3 to choose ur monster"<<endl; cin>>choice; creature foo(choice); 正如juanchopanz
This question already has an answer here: Dynamically allocating an array of objects 7 answers What is The Rule of Three? 8 answers This is because you make copies of your Element when you push it in the vector, but the vtx is not duplicated on copy, so an the end of main(), you will have three Elements pointing to the same vtx. When the program terminates, all three of them will try to d
这个问题在这里已经有了答案: 动态分配一个对象数组7个答案 什么是三项规则? 8个答案 这是因为当你在向量中推送元素时,你创建了元素的拷贝,但是vtx没有在拷贝上被复制,所以在main()的末尾,你将有三个元素指向相同的vtx。 当程序终止时,它们三个都会尝试删除相同的int数组。 将elm[0]添加到vec , elm[0]副本将存储在vec 。 由于您尚未定义复制构造函数,因此编译器使用默认值 - 它通过成员复制执行成员。
Possible Duplicate: What is The Rule of Three? When you require to define to your own assignment operator? Generally, you'll need to define your own assignment operator under the same circumstances when you need to define your own copy constructor - ie when a default copy won't cut it. This happens in cases when your object manages dynamically allocated memory or other resources wh
可能重复: 什么是三项规则? 当你需要定义你自己的赋值操作符时? 一般来说,当你需要定义自己的拷贝构造函数时,你需要在相同的情况下定义你自己的赋值运算符 - 也就是说,当缺省拷贝不会削减它时。 当您的对象管理动态分配的内存或需要专门复制的其他资源时,会发生这种情况。 例如,如果您有一个管理指向动态分配内存的指针的类,则缺省赋值操作符将简单地复制该指针。 通常,这不是您想要的 - 您希望每个对象实例
Possible Duplicate: What is The Rule of Three? I have the a problem of the double freeing of memory in the following program. The debugger shows that the issue is in the push_back() function. Class A: class A { public: A(int x); int x; }; A::A(int x) { this->x = x; } Class B: class B { public: B(int x); ~B(); A* a; }; B::B(int x)
可能重复: 什么是三项规则? 在下面的程序中,我有一个双重释放内存的问题。 调试器显示问题出在push_back()函数中。 A类: class A { public: A(int x); int x; }; A::A(int x) { this->x = x; } B类: class B { public: B(int x); ~B(); A* a; }; B::B(int x) { this->a = new A(x); } B::~B() { delete a; } 主功能: int main() {
Possible Duplicate: What is The Rule of Three? How exactly does std::pair call destructors for its components? I am trying to add instances of a class to an std::map , but I am getting errors regarding the destructor of my class. I have narrowed down my question/problem to the following extremely simple example. Below, my_class merely creates an int array at construction, and deletes it
可能重复: 什么是三项规则? std::pair如何为其组件调用析构函数? 我正在尝试向std::map添加一个类的实例,但是我得到了有关我的类的析构函数的错误。 我已经将我的问题缩小到以下非常简单的例子。 下面, my_class只是在构造时创建一个int数组,并在销毁时删除它。 不知何故,我得到一个“双删除”的错误: //my_class.h class my_class { public: int an_int; int *array; //constructors: my_cl
This question already has an answer here: What is The Rule of Three? 8 answers The rule of Three and The Rule of Zero The good ol' way of handling resources was with the Rule of Three (now Rule of Five due to move semantic), but recently another rule is taking over: the Rule of Zero. The idea, but you should really read the article, is that resource management should be left to other
这个问题在这里已经有了答案: 什么是三项规则? 8个答案 三大规则与零规则 处理资源的好方法是用三条规则(现在的规则是因为移动语义),但最近另一条规则正在接管:零规则。 这个想法,但你应该真正阅读的文章是资源管理应该留给其他特定的类。 在这方面,标准库提供了一组好的工具,如: std::vector , std::string , std::unique_ptr和std::shared_ptr ,有效地消除了对自定义析构函数的需求,移动/复制构造函
Can't a compiler warn (even better if it throws errors) when it notices a statement with undefined/unspecified/implementation-defined behaviour? Probably to flag a statement as error, the standard should say so, but it can warn the coder at least. Is there any technical difficulties in implementing such an option? Or is it merely impossible? Reason I got this question is, in statements
当它发现一个含有未定义/未指定/实现定义行为的语句时,编译器是否会发出警告(如果它引发错误,则更好)? 可能将一个声明标记为错误,标准应该这样说,但它至少可以警告编码人员。 实施这样的选择有没有技术上的困难? 还是仅仅是不可能的? 我得到这个问题的原因是,在像a[i] = ++i;这样a[i] = ++i;陈述中a[i] = ++i; 在达到序列点之前,不会知道代码试图引用变量并在同一语句中修改它。 这一切归结为 实施质量:
I was going through some of the questions on constructor initialization list when I bumped into this. Consider this: class Student { public: Student() { id = 0; } Student(int i) { id = i; } private: int id; }; Now, check this out: By the time you get in the body of the constructor, all fields have already been construct
当我遇到这个问题时,我正在浏览构造函数初始化列表中的一些问题。 考虑这个: class Student { public: Student() { id = 0; } Student(int i) { id = i; } private: int id; }; 现在,检查一下: 当你进入构造函数的主体时,所有的字段都已经被构造了; 如果他们有默认的构造函数,那些已经被调用。 现在,如果您在构造函数的主体中为它们赋
I've read that The Rule of Three, What is The Rule of Three? is summarized as follows: If you need to explicitly declare either the destructor, copy constructor or copy assignment operator yourself, you probably need to explicitly declare all three of them. My question is: In a C++ application, I have a class that manages resources (has a destructor that handles deleting pointers). I kn
我读过三条法则,什么是三条法则? 总结如下: 如果您需要自己显式声明析构函数,复制构造函数或复制赋值运算符,则可能需要明确声明它们中的全部三个。 我的问题是:在C ++应用程序中,我有一个管理资源的类(具有处理删除指针的析构函数)。 我知道应用程序在整个地方都使用了赋值运算符,但我绝对确信在复制构造函数的应用程序中没有用法,即使用Class c(..); Class d(c);类型Class c(..); Class d(c); Class c(..); Cl
UPDATE at the bottom q1: How would you implement the rule of five for a class that manages rather heavy resources, but of which you want it to be passed around by value because that greatly simplifies and beautifies it's usage? Or are not all five items of the rule even needed? In practice, I'm starting something with 3D imaging where an image is usually 128*128*128 doubles. Being a
在底部更新 问题1:如何为一个管理相当重的资源的类实施五项规则,但是您希望它按价值传递,因为这极大地简化和美化了它的用法。 或者不是规则的全部五项甚至需要? 在实践中,我开始使用3D成像,其中图像通常是128 * 128 * 128加倍。 尽管写这样的东西会使数学变得更容易: Data a = MakeData(); Data c = 5 * a + ( 1 + MakeMoreData() ) / 3; q2:使用复制elision / RVO /移动语义的组合,编译器应该能够以最少的复制