destructor

Dealing with protected/private constructor/destructor for a CRTP design?

守給你的承諾、 提交于 2019-12-05 22:38:16
Consider the following code: #include <iostream> #include <type_traits> // Abstract base class template<class Crtp> class Base { // Lifecycle public: // MARKER 1 Base(const int x) : _x(x) {} protected: // MARKER 2 ~Base() {} // Functions public: int get() {return _x;} Crtp& set(const int x) {_x = x; return static_cast<Crtp&>(*this);} // Data members protected: int _x; }; // Derived class class Derived : public Base<Derived> { // Lifecycle public: Derived(const int x) : Base<Derived>(x) {} ~Derived() {} }; // Main int main() { Derived d(5); std::cout<<d.set(42).get()<<std::endl; return 0; } If

Destructor in template class c : How to delete field which may be pointer or not pointer?

泪湿孤枕 提交于 2019-12-05 22:18:32
I have template class Array where the template type T should be either pointer or not-pointer type. template<class T> class TArray { static const int max_len=100; T* data; int length; public: TArray(){data=new T[max_len]; this->length=0;} void Add(T value){data[length++]=value;} ~TArray(); }; The problem is how to free space, since we can not call delete for not pointer types like this template<class T> TArray<T>::~TArray() { //for(int i =0; i < length; i++) // delete data[i]; // NOT WORKING WITH OBJECTS THAT ARE NOT POINTERS !!! delete[] data; } Let's have addition class class A { int* a;

Disposing of SQL Connection

本秂侑毒 提交于 2019-12-05 19:32:16
I have a SQL class that connects to the DB and retreives a DataTable. I am aware that the SqlConnection must be disposed when finished. I know this can be done using a using block, but is it also acceptable to put the Dispose() call inside the destructor of this class? Herre is my code: public class SQLEng { //Connection String Property //Must be set to establish a connection to the database public string ConnectionString{ get; set; } SqlConnection _Conn; //Overridden Constructor enforcing the Connection string to be set when created public SQLEng(string connectionString) { ConnectionString =

Destructor of class with pointer array C++

血红的双手。 提交于 2019-12-05 18:13:17
If I have a class with an array of pointers to another class Vehicle : class List { public: //stuff goes here private: Vehicle ** vehicles; } If I now write the destructor of the class List , do I manually iterate over the array (I know how many items are in the array) and delete every pointer to a vehicle, or will C++ automatically call the destructors of all the Vehicles in the array? (Like it does if there's a private string/... in the class or if it would be a STL container of Vehicle pointers) EDIT: I forgot about delete [] vehicles , but if I would do that, would it also delete the

Destructor called on assignment between (stack) variables?

北城余情 提交于 2019-12-05 17:39:00
matrix m1(5,5); matrix m2(5,5); m1 = matrix(m2); For the above code (for an arbitrary class, matrix), will the destructor be called for the information associated with m1, when the information of m2 is copied over to it? No, an assignment operator will need to deal with releasing whatever resources m1 may hold before performing an assignment. The destructor will be called only when m1 is about to go out of scope. No, once an object which is allocated on the stack is constructed it isn't deconstructed until it either goes out of scope or you call its destructor explicitly (which you should

Destructor not called after destroying object placement-new'ed

蹲街弑〆低调 提交于 2019-12-05 16:42:58
问题 I had no clue why this doesn't work. The following Function is created by placement new. A function is provided that checks whether it should be destructed, and if so, calls its destructor manually. Here is the testcase where it seems the destructor is never called: /* Represents a function at runtime */ class Function { public: /* Creates an invalid function */ Function():codeptr(0) { } /* Creates a function with the given code pointer */ Function(void *codeptr):codeptr(codeptr) { } /* Frees

Purpose of static_initialization_and_destruction and _GLOBAL__sub_I_main function in the assembly code for a C++ code?

房东的猫 提交于 2019-12-05 14:38:52
The following is the C++ source code. The code has a class HumanBeing and with Display and verify functions. Each function prints statements. #include <iostream> using namespace std; class HumanBeing { public: void display() { cout << "hello aam a human being" << endl; } void print() { cout << "verify print" << endl; } }; int main() { HumanBeing vamshi; vamshi.display(); vamshi.print(); return 0; } This is the corresponding assembly code of the above c++ code .file "verify.cpp" .local _ZStL8__ioinit .comm _ZStL8__ioinit,1,1 .section .rodata .LC0: .string "hello aam a human being" .section

C++: Concurrency and destructors

核能气质少年 提交于 2019-12-05 13:55:46
Suppose you have an object which can be accesed by many threads. A critical section is used to protect the sensitive areas. But what about the destructor? Even if I enter a critical section as soon as I enter the destructor, once the destructor has been called, is the object already invalidated? My train of thought: Say I enter the destructor, and I have to wait on the critical section because some other thread is still using it. Once he is done, I can finish destroying the object. Does this make sense? In general, you should not destroy an object until you know that no other thread is using

failed constructor and failed destructor in C++

南楼画角 提交于 2019-12-05 13:53:26
I have one question about failed constructor and failed destructor in C++. I noticed that when the constructor failed, an exception will be thrown. But there is no exception thrown in destructor. My question is 1) If constructor failed, what exception will be thrown? bad_alloc? or anything else related? Under what situation, a constructor would fail? What about the successfully constructed part? 2) Under what situation, a destructor would fail? If no exception is thrown, what would happen to the destructor? How does the compiler deal with it? What's the return value to the function it is

Why the Destructor in C++ de-allocated memory in reverse order of how they were initialised?

无人久伴 提交于 2019-12-05 12:48:39
问题 What is the advantage in de-allocating memory in reverse order to variables? 回答1: Consider this example: Type1 Object1; Type2 Object2(Object1); Suppose that Object2 uses some internal resources of Object1 and is valid as long as Object1 is valid. For example, Object2 s destructor accesses Object1 's internal resource. If it weren't for the guarantee of reverse order of destruction, this would lead to problems. 回答2: It's not just about deallocating memory, it's about symmetry in a broader