destructor

Deleting derived classes in std::unique_ptr<Base> containers

那年仲夏 提交于 2019-12-01 00:48:28
I'm a little confused. Basically, I've got 2 different resource managers (AudioLibrary and VideoLibrary) that both inherit from a shared BaseLibrary class. This base class contains references to both audio and video. Both audio and video inherit from a parent class called Media. I'm keeping the data in a map, filled with unique_ptr. But, to my surprise, I've discovered when these pointers are eventually deleted via .erase, only the base destructor for Media is called. I guess I've missed something, but I thought that the compiler/run-time library would know that it's either pointing to a video

Practical application of class destructor

爱⌒轻易说出口 提交于 2019-11-30 23:45:46
I'm currently trying to learn about classes and constructors/destructors. I understand what the two do, but I'm having a harder time with the destructors because I can't think of a practical application for its use. Can anyone provide an example with an explanation? Destructors are special member functions used to release any resources allocated by the object. The most common example is when the constructor of the class uses new , and the destructor uses delete to deallocate the memory. class Myclass { int *m_ptr; public: Myclass():m_ptr(new int) { } ~Myclass() { delete m_ptr; } //ToDo: Follow

Could someone explain this C++ union example?

空扰寡人 提交于 2019-11-30 23:24:17
I found this code on cppreference.com. It's the strangest C++ I've seen, and I have a few questions about it: union S { std::string str; std::vector<int> vec; ~S() {} }; int main() { S s = { "Hello, world" }; // at this point, reading from s.vec is undefined behavior std::cout << "s.str = " << s.str << '\n'; s.str.~basic_string<char>(); new (&s.vec) std::vector<int>; // now, s.vec is the active member of the union s.vec.push_back(10); std::cout << s.vec.size() << '\n'; s.vec.~vector<int>(); } I want to make sure I've got a few things right. The union forces you to initialise one of the union

Python destructors in new and old style classes [duplicate]

感情迁移 提交于 2019-11-30 21:30:52
This question already has an answer here: Why do new style class and old style class have different behavior in this case? 1 answer I'm trying to understand why object destruction works differently in new style classes compared to old style ones. class Wrapper(): class Inner(object): def __del__(self): print 'Inner destructor' innerInstance = Inner() def __del__(self): print 'Wrapper destructor' if __name__ == '__main__': x = Wrapper() on exit, this will output: Wrapper destructor Inner destructor however, if i use Wrapper as a new style class, only the wrapper destructor is called, and the

Object destruction problem with MEF

混江龙づ霸主 提交于 2019-11-30 21:06:37
问题 I use a static variable for holding the count of objects. In constructor I increase this variable. This way I know how many instances of the object are created. After using the objects, they are leaved dereferenced. I'm doubtful if MEF is holding references to these objects so I force GC to do a clean up (Using GC.Collect() method). I expect on next object creation this variable start from zero but it resumes from last number. I put a logging mechanism in destructor for tracing, and objects

why is a scalar deleting destructor being called as a result of vector delete on Windows?

风格不统一 提交于 2019-11-30 20:32:16
I have a code that is leaking on Windows. It runs fine on many unix platforms and the leak only occurs on Windows. The binary consists of exe, 1 dll and 2 static libs. The exe links to both the dll and the static libs, while the static libs link with the dll as well. The leak occurs in the exe code when instead of calling to a vector deleting destructor, for some reason scalar deleting destructor is called. This results in only the first object in the array to be deleted while the rest of the array stays in memory. The leaking pseudo-code looks like this: class MyClassFromExe : public

Python destructors in new and old style classes [duplicate]

喜夏-厌秋 提交于 2019-11-30 17:29:46
问题 This question already has an answer here : Why do new style class and old style class have different behavior in this case? (1 answer) Closed 4 years ago . I'm trying to understand why object destruction works differently in new style classes compared to old style ones. class Wrapper(): class Inner(object): def __del__(self): print 'Inner destructor' innerInstance = Inner() def __del__(self): print 'Wrapper destructor' if __name__ == '__main__': x = Wrapper() on exit, this will output:

How do I get the member function pointer of a destructor?

[亡魂溺海] 提交于 2019-11-30 17:27:45
问题 Assume I have struct X { ~X() {} }; What's the type of and how do I get the member function pointer of X::~X() in C++03? I don't want to actually call it, just use in SFINAE to figure if there exists a destructor for a given type. 回答1: You can't get the function pointer of a destructor nor a constructor. Nevertheless a destructor always exist for a type, and you can't detect if its private with as access specifiers are not considered by SFINAE . On the subject of invoking what would be the

Why can I not call my class's constructor from an instance of that class in C++?

一世执手 提交于 2019-11-30 16:58:38
When can an object of a class call the destructor of that class, as if it's a regular function? Why can't it call the constructor of the same class, as one of its regular functions? Why does the compiler stops us from doing this? For example: class c { public: void add() ; c(); ~c() ; }; void main() { c objC ; objC.add() ; objC.~c() ; // this line compiles objC.c() ; // compilation error } By definition, a constructor is only called once, when the object is created. If you have access to an object, then it must have been created, so you're not allowed to call the constructor again - this is

Do we need to explicitly call the destructor for the “simple POD classes” allocated with “placement new”?

社会主义新天地 提交于 2019-11-30 16:51:53
问题 Here by "simple", I mean a class with non-virtual empty destructor or POD type. Typical example: char buffer[SIZE]; T *p = new(buffer) T; ... p->~T(); // <---- always ? What happens if we don't call the explicit destructor on p ? I don't think it is undefined behavior or memory leak. Is there any problem with reusing buffer ? 回答1: For a POD-type or a class with a trivial destructor: no. The lifetime of the object will end when the storage for the object is released or reused. You don't have