Consider the case of class which does not have a destructor and constructor explicitly declared by the developer. I understand that a destructor for a class will be implicitly declared in this case. Then is it true that the destructor is implicitly defined, only when an object of the class is about to be destroyed?
Is the behavior of constructor also the same as above. Is it implicitly defined only when an object of the class is created?
EDIT
class A {
public:
};
int main() {
}
In the above code, ~A() will be implicitly declared. My question is whether it true that the definition for the destructor will be made implicitly, only if an object of the class is instantiated like
class A {
public:
};
int main() {
A a;
}
Or is it implicitly defined, even if object instantiation is not done ?
Yes, implicitly declared default constructors and destructors are implicitly defined when they are used to create or destroy instances of the object. In the words of the standard (C++11):
So they are defined in your second code snippet, which creates and destroys an object of type
A, but not in the first, which doesn’t.