C++ Programming - Pseudo Destructors

Fundamental types have constructors, as you have seen. In addition, fundamental types also have a pseudo destructor. A pseudo destructor is a syntactic construct whose sole purpose is to satisfy the need of generic algorithms and containers. It is a no-op code that has no real effect on its object. If you examine the assembly code that your compiler produces for a pseudo destructor invocation, you might discover that the compiler simply ignored it. A pseudo destructor invocation is shown in the following example:

typedef int N;
int main()
{
N i = 0;
i.N::~N(); //pseudo destructor invocation
i = 1; //i was not affected by the invocation of the pseudo destructor
return 0;
}

The variable i is defined and initialized. In the following statement, the pseudo destructor of the non-class type N is explicitly invoked but it has no effect on its object. Like the constructors of fundamental types, pseudo destructors enable the user to write code without having to know if a destructor actually exists for a given type.

Labels:


About this entry