destructor

Why does the insertion of user defined destructor require an user defined copy constructor

偶尔善良 提交于 2019-12-20 02:44:15
问题 The following code compiles: #include <vector> #include <iostream> #include <memory> using namespace std; class container { public: container(){} ~container(){} }; class Ship { public: Ship(){} //Ship(const Ship & other){cout<<"COPY"<<endl;} //~Ship(){} std::unique_ptr<container> up; }; Ship buildShip() { Ship tmp; return tmp; } int main(int argc, char *argv[]) { return 0; } But if we include the user defined destructor ~Ship(){} , the code will only compile if we also include the user

About constructors/destructors and new/delete operators in C++ for custom objects

好久不见. 提交于 2019-12-20 02:35:14
问题 Suppose I have a Linked List I created myself. It has its own destructor, which frees the memory. This Linked List does not overload new or delete. Now, I'm trying to create an array of said linked lists (open hashing, if I understand correctly). Then I allocate the necessary memory inside the constructor of this open hashing class. The new operator being called inside the constructor is enough to correctly allocate the memory for the array, right? I'm not sure because I haven't overloaded

Destructor is called when I push_back to the vector

孤街浪徒 提交于 2019-12-19 10:37:21
问题 I have this class definition: class FlashStream { public: explicit FlashStream(const char * url, vector<uint8> * headers, vector<uint8> * data, void * ndata, void * notifyData = NULL, uint32 lastModified = NULL); ~FlashStream(); private: NPStream _stream; // ... } (NPStream description) and its implemetation: FlashStream::FlashStream(const char * url, vector<uint8> * headers, vector<uint8> * data, void * ndata, void * notifyData, uint32 lastModified) { // ... memset(&_stream, 0, sizeof

Operator new[] does not receive extra bytes

限于喜欢 提交于 2019-12-19 06:39:32
问题 I have such code #include <cstdlib> class Foo { int m_data; public : Foo() : m_data(0) { } /*~Foo() { }*/ static void* operator new[](const size_t size) { return malloc(size); } static void operator delete[](void* data) { free(data); } }; int main() { Foo* objects = new Foo[5]; delete [] objects; } In this case I receive value of size in operator new overloading as 20 bytes as I wanted ( sizeof(int) * 5 ). But if I uncomment the destructor I get size as 24 bytes. Yeah, I now that these extra

Does destroying and recreating an object make all pointers to this object invalid?

南笙酒味 提交于 2019-12-19 05:57:07
问题 This is a follow-up to this question. Suppose I have this code: class Class { public virtual method() { this->~Class(); new( this ) Class(); } }; Class* object = new Class(); object->method(); delete object; which is a simplified version of what this answer suggests. Now once a destructor is invoked from within method() the object lifetime ends and the pointer variable object in the calling code becomes invalid. Then the new object gets created at the same location. Does this make the pointer

Pure virtual invocation from constructor and destructor

瘦欲@ 提交于 2019-12-19 05:08:31
问题 The C++ standard says that invoking a pure virtual function from a constructor or destructor is forbidden. What is the reason for this? Why should the standard place a restriction like this? 回答1: At the point a class destructor is run, all subclass destructors have already been run. It would not be valid to call a virtual method defined by a subclass, for which its destructor has already run. A similar restriction exists around calling virtual methods in constructors. You can't call a virtual

Could someone explain this C++ union example?

狂风中的少年 提交于 2019-12-19 04:00:35
问题 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>(); }

Could someone explain this C++ union example?

烂漫一生 提交于 2019-12-19 04:00:03
问题 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>(); }

When is a function registered with atexit() called

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 21:20:15
问题 I want to know if functions registered with atexit() are called before or after global variables are destroyed. Is this specified by the standard or implementation defined? 回答1: It is well-defined, and depends on whether the object in question was constructed before or after the function got registered using atexit() : 3.6.3 Termination 3. If the completion of the initialization of an object with static storage duration is sequenced before a call to std::atexit (see <cstdlib> , 18.5), the

How do I call the class's destructor?

烈酒焚心 提交于 2019-12-18 21:17:38
问题 I have a simple C++ code, but I don't know how to use the destructor: class date { public: int day; date(int m) { day =m; } ~date(){ cout << "I wish you have entered the year \n" << day; } }; int main() { date ob2(12); ob2.~date(); cout << ob2.day; return 0; } The question that I have is, what should I write in my destructor code, that after calling the destructor, it will delete the day variable ? 回答1: You should not call your destructor explicitly. When you create your object on the stack