C++ basic constructors/vectors problem (1 constructor, 2 destructors)

前端 未结 2 1062
借酒劲吻你
借酒劲吻你 2020-12-11 10:24

Question is probably pretty basic, but can\'t find out what\'s wrong (and it leads to huge of memleaks in my app):

class MyClass {
public:
    MyClass() { co         


        
2条回答
  •  臣服心动
    2020-12-11 10:54

    When the b object gets pushed onto the vector a copy is made, but not by the operator=() you have - the compiler generated copy constructor is used.

    When the main() goes out of scope, the b object is destroyed and the copy in the vector is destroyed.

    Add an explicit copy constructor to see this:

    MyClass( MyClass const& other) {
        cout << "copy ctor\n";
    };
    

提交回复
热议问题