This question already has answers here:
Does destroying and recreating an object make all pointers to this object invalid?
(6 answers)
Is it allowed to call destructor explicitly followed by placement new on a variable with fixed lifetime?
(2 answers)
Closed 4 years ago.
Suppose I have a class like so:
#include <new>
struct thing {
thing() = default;
void foo()
{
this->~thing();
new (this) thing();
}
};
Is calling the destructor like this and then reconstructing the object using a placement new defined behavior?
Related
This question already has answers here:
What is the point of deleted destructor?
(1 answer)
How does =delete on destructor prevent stack allocation?
(6 answers)
Closed 4 years ago.
We can define a class with its destructor deleted:
class A {
public :
A() = default;
~A() = delete;
};
Then, we cannot define a variable of type A.
However, we can use new operator to allocate and construct an object of type A in the heap and we can never delete it.
I want to know whether this class has any usage in the real-world?
This question already has answers here:
Why is there no call to the constructor? [duplicate]
(3 answers)
Closed 5 years ago.
Class A
{
public:
A()
{
cout << "constructor called";
}
};
int main()
{
A obj; // constructor getting called
A obj1(); // constructor not getting called
}
When i instantiate obj object , my constructor getting called.
But when i instantiate obj1 , my constructor not getting called.
I would like to know the reason for it.
A obj1(); actually does not create any object, it's a function declaration.
() should be avoided here as the compiler can't tell if you meant to create a A object or declare a obj1 function returning a A object.
This question already has answers here:
c++ deleting vector class member memory in destructor
(4 answers)
Closed 6 years ago.
Say I have such a class
class A {
public:
vector<int> intVector;
A() {
intVector.push_back(1);
};
~A(){};
};
int main() {
A *k = new A;
delete k;
}
When A is instantiated as k, intVector will be created. However, my question is, if the destructor does not take care of intVector, will the memory allocated for intVector be recycled when the pointer of k is destroyed?
Yes, the compiler will generate code inside the destructor of the class to destroy all member variables (and call any base-class destructors that a class is derived from)
This question already has answers here:
Is there a difference between copy initialization and direct initialization?
(9 answers)
Closed 7 years ago.
If a class is having constructor which will take parameters as below
class myclass
{
public:
myclass(int a, int b);
};
what are the differences between creating objects as below
myclass a(3, 4);
myclass a = myclass(3, 4);
and which one is better?
The first one is better and recommended. The second one creates a temporary then uses copy construction, although most of the time the copy is elided.
This question already has answers here:
Do the parentheses after the type name make a difference with new?
(7 answers)
Default constructor with empty brackets
(9 answers)
different types of initialization in C++
(2 answers)
Closed 10 years ago.
class A {
public:
A() { cout << "Constructor\n"; } // (1) default constructor
};
A obj; // (2) instantiating obj
A obj(); // (3)
What is the difference between instantiating obj and obj()? obj calls the default constructor (1) mentioned above. Which constructor will obj() call?
A obj(); declares a function called obj which takes no arguments and which returns an A. It does not declare an A object at all.
As A obj(); does not declare an A object, it does not result in any constructor call.
The second one is declared as a function. the name of the function is obj. It takes no arguments. It returns the object of type A.