destructor

Why do we need to use virtual ~A() = default; instead of virtual ~A() {} in C++11?

爷,独闯天下 提交于 2019-11-29 21:15:18
In Stack Overflow post Checking the object type in C++11 , I have the comment: In C++11 you'll actually want to do virtual ~A() = default; Otherwise, you'll lose the implict move constructors. What is virtual ~A() = default; for? How come implicit move constructors lost with virtual ~A() {} ? The comment is not correct. Both: virtual ~A() = default; and virtual ~A() {} are user declared . And the implicit move members are inhibited if the destructor is user declared. [dcl.fct.def.default]/p4 discusses user-declared and user-provided special members: A special member function is user-provided

C++ Suppress Automatic Initialization and Destruction

北城以北 提交于 2019-11-29 21:05:32
问题 How does one suppress the automatic initialization and destruction of a type? While it is wonderful that T buffer[100] automatically initializes all the elements of buffer , and destroys them when they fall out of scope, this is not the behavior I want. #include <iostream> static int created = 0, destroyed = 0; struct S { S() { ++created; } ~S() { ++destroyed; } }; template <typename T, size_t KCount> class fixed_vector { private: T m_buffer[KCount]; public: fixed_vector() { // some way to

Parameter “size” of member operator new[] increases if class has destructor/delete[]

元气小坏坏 提交于 2019-11-29 19:19:56
问题 4 classes in the following codes: A, B, C and D. They all have a member operator new[] . Besides, B has a constructor; C has a destructor; D has a member operator delete[] . The Parameter size of member operator new[] and the sizeof of the 4 classes are output: new[] A 40 new[] B 40 new[] C 48 new[] D 48 sizeof(A) 4 sizeof(B) 4 sizeof(C) 4 sizeof(D) 4 What's the reason for the differences of size ? Codes(ugly I know): #include <iostream> using namespace std; class A { int i; public: static

What is a non-trivial destructor in C++?

送分小仙女□ 提交于 2019-11-29 17:38:08
问题 I was reading this which mentions destructors being trivial and non-trivial. A class has a non-trivial destructor if it either has an explicitly defined destructor, or if it has a member object or a base class that has a non-trivial destructor. In example, I have a class, class C { public: ~C(); // not explicitly declared. }; If C::~C() is implicitly defined does it make a trival dtor? 回答1: You are getting your words mixed up. Your example does indeed declare an explicit destructor. You just

When do I need to declare my own destructor?

梦想与她 提交于 2019-11-29 17:03:17
问题 class Point { public: float x,y; Point() {} Point(float,float); Point operator + (Point); Point operator * (double); void rotate_p(float); void render_p(Point*); void sub(float); float get_dist();//get_distance }; As you can see this class has no pointers as non-static data-members , so I think I can just use the default destructor; is this accurate? Question When do I need to declare my own destructor? 回答1: Introduction Since data-members with automatic-storage duration have their lifetime

Why is the destructor not called for the returned object from the function?

旧城冷巷雨未停 提交于 2019-11-29 16:42:47
问题 I was thinking that when a function returns an object on the stack to the calling function, the calling function gets a copy of the original object but the original object's destructor is called as soon as the stack unwinds. But in the following program, the destructor is getting called only once. I expected it to be called twice. #include <iostream> class MyClass { public: ~MyClass() { std::cout << "destructor of MyClass" << std::endl; } }; MyClass getMyClass() { MyClass obj = MyClass();

Destructor in virtual inheritance

天大地大妈咪最大 提交于 2019-11-29 16:37:12
class Base{}; class D1:virtual public Base{}; class D2:virtual public Base{}; class DD:public D1,public D2{}; int main(){ Base *pBase=new DD; delete pBase; } This leads to crash, but I modify as below: class Base{ public: virtual ~Base(){}; }; class D1:virtual public Base{ public: virtual ~D1(){} }; class D2:virtual public Base{ public: virtual ~D2(){} }; class DD:public D1,public D2{ }; Then, it passes, but the default destructor should be the virtual dummy function, isn't it? From the C++11 specification (ISO/IEC 14882:2011(E)), section 12.4 Destructors [class.dtor]: Sub-section 4: If a

Detecting when destructor running due to exception being thrown?

耗尽温柔 提交于 2019-11-29 16:32:56
问题 What is a good way in C++ to detect in a destructor that it is being run during unwind of the stack due to an exception being thrown as opposed to a normal exit of scope triggering the destructor? I'd like to know so that I can create a class that has some cleanup code that is always run on normal exit but skipped when an exception occurs. 回答1: std::uncaught_exception() (defined in <exception> ) will tell you in your destructor if it was called because of an exception: class A { public: ~A()

How can I call a private destructor from a shared_ptr?

拜拜、爱过 提交于 2019-11-29 14:20:53
问题 I have a resource_manager class which maintains a std::vector<boost::shared_ptr<resource> > internally. resource_manager is a friend class of resource . I want resource s to only be created/deleted by resource_manager , so I made its constructors private (which works ok). However, if I make the destructor private, the code doesn't compile because the destructor is called by boost::shared_ptr , which is not a friend of resource . I am thinking of enforcing the "do not delete by clients" rule

Why garbage collector takes objects in the wrong order?

a 夏天 提交于 2019-11-29 13:54:47
I have an application with two classes, A and B. The class A has inside a reference to class B. The destructors of the classes do some cleanup of resources but they have to be called in the right order, first the destructor of A and then the destructor of B. What is happening is that somehow the destructor of B is called first and then the destructor of A is crashing because is trying to execute methods from a disposed object. Is this behavior of the GC correct? I expected the GC to detect that A has a reference to B and then call the A destructor first. Am I right? Thanks mates! PD: In case