destructor

c++ Copy constructors and destructors

别说谁变了你拦得住时间么 提交于 2019-12-13 11:07:25
问题 I am learning constructors and Destructors in c++; Help me grasp my mistakes even if they are silly... HERE is a code I have written to perform addition using classes in c++; This creates two summands of datatype num and employs the constructor sum() to perform sum of the two numbers; However when everything was goin' alright, I stumbled upon creating a copy constructor for num , (Although not necessary but still for practice)... without the dynamic object of the class sum it is not possible

Return Statement Returning a Null Pointer Value and Not the Desired Value

ⅰ亾dé卋堺 提交于 2019-12-13 09:00:01
问题 I ran this through debug, and in the String Substring function, everything works up until the return statement. 'returnString', in the code below, has the correct value when at the return line. However, as soon as I go to next line (the closing bracket directly after), it changes to: {Text=0x003ed0e0

Using placement new, malloc, and free

耗尽温柔 提交于 2019-12-13 08:28:34
问题 Basically, I have a block of memory allocated using malloc that I want to start placing objects into using placement new. I know that the destructors for these objects will have to be explicitly called when I'm deleting them, but I want to make sure that I understand this completely, and that I'm going about it the right way. I'm almost certainly going to scrap this approach and go about things in a more straightforward manner, but I figured that I'd ask just for understanding's sake. I have

Calling destructor with decltype and\or std::remove_reference

丶灬走出姿态 提交于 2019-12-12 21:17:48
问题 Is it possible to call destructor(without operator delete) using decltype and\or std::remove_reference? Here's an example: #include <iostream> #include <type_traits> using namespace std; class Test { public: Test() {} virtual ~Test() {} }; int main() { Test *ptr; ptr->~Test(); // works ptr->~decltype(*ptr)(); // doesn't work ptr->~std::remove_reference<decltype(*ptr)>::type(); // doesn't work return 0; } 回答1: You can use an alias template to get an unqualified type name when all you have is a

C# class: Do logging / housekeeping, should I use a destructor?

为君一笑 提交于 2019-12-12 18:01:16
问题 I have a c# class. Whenever this class is not in use anymore I want to do some things. For example log the current state and so on. I want to be sure that this method is run everytime when the class is not used anymore. I don't want just use a simple method because I can't be sure that every user is calling it. I have no resources (like file handles) to clear up. Is the best way to use a destructor? "not in use" is when (for example): a user uses my class in a form and the form is closed the

A few memory management questions involving class destructors and delete operator?

纵然是瞬间 提交于 2019-12-12 13:32:17
问题 After reading some tutorials I am still unclear on some points about memory management in C++. 1. when a class declared with the new operator goes out of scope is its destructor called and memory freed? Is it necessary to call the delete operator to free the class' memory and have its destructor called? class Test{}; void newTest(){ Test *t = new Test; } int main() { newTest(); return 0; } 2. Are variables (such as a vector) declared with the new keyword freed when the class they reside

Passing by reference - why is this destructor being called?

元气小坏坏 提交于 2019-12-12 12:43:02
问题 I could not find (of the many questions on destructor calling topics) any that were exactly the same as my situation. Why is the destructor being called when the parameter passed is a reference? I put comments (mostly in main) under the lines of code where I thought the output was executing from. struct X { // simple test class int val; void out(const std::string& s, int nv) { std::cerr << this << "–>" << s << ": " << val << " (" << nv << ")\n"; } // default constructor X() { out("X()", 0);

Are non-static class members destroyed even without a destructor?

巧了我就是萌 提交于 2019-12-12 11:03:34
问题 In Bjarne Stroustrup's "The C++ Programming Language (4th edition)" in section 17.6 (Generating Default Operations) it mentions this: If the programmer declares a copy operation, a move operation, or a destructor for a class, no copy operation, move operation, or destructor is generated for that class. Thus, I'm confused why the SubObj destructor is called in this program: #include <iostream> using namespace std; class SubObj { public: ~SubObj() { cout << "SubObj Destructor called" << endl; }

Generic binary tree node destructor issue

梦想与她 提交于 2019-12-12 10:01:53
问题 I've been working on an assignment and now I'm stuck with buggy destructors. I have to create a generic binary tree with all the usual member functions and some special operators. There's also a restriction: everything must work iteratively so no nasty recursive hacks this time. There is obviously something very wrong with the destructor of BinTreeNode class because if I delete the node like this: BinTreeNode<int> * node = new BinTreeNode<int>(); delete node; I can still access its data: node

Calling std::~basic_string() in gdb

别说谁变了你拦得住时间么 提交于 2019-12-12 09:58:08
问题 As per @EvanED in https://stackoverflow.com/a/11311786/890753 I created a gdb command newstr to create a new std::string and put it in a gdb convenience variable: define newstr set ($arg0)=(std::string*)malloc(sizeof(std::string)) call ($arg0)->basic_string() # 'assign' returns *this; casting return to void avoids printing of the struct. call (void)( ($arg0)->assign($arg1) ) end It works great: (gdb) newstr $foo "hello world" (gdb) p $foo->c_str() $57 = 0xb22e388 "hello world" I use newstr in