Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are the private destructors of static objects called? [duplicate]

Possible Duplicate:
Cannot access private member in singleton class destructor

I'm implementing a singleton as below.

class A
{
public:

    static A& instance();
private:
    A(void)
    {
        cout << "In the constructor" << endl;
    }
    ~A(void)
    {
        cout << "In the destructor" << endl;
    }

};

A& A::instance()
{
    static A theMainInstance;
    return theMainInstance;
}

int main()
{
    A& a = A::instance();

    return 0;
 }

The destructor is private. Will this get called for the object theMainInstance when the program is about to terminate?

I tried this in Visual studio 6, it gave a compilation error.

"cannot access private member declared in class..."

In visual studio 2010, this got compiled and the destructor was called.

What should be the expectation here according to the standard?

Edit : The confusion arises since Visual Studio 6 behaviour is not so dumb. It can be argued that the constructor of A for the static object is called in the context of a function of A. But the destructor is not called in the context of the same function. This is called from a global context.

like image 910
PermanentGuest Avatar asked Jul 17 '12 14:07

PermanentGuest


People also ask

What happens when a destructor is private?

Destructors with the access modifier as private are known as Private Destructors. Whenever we want to prevent the destruction of an object, we can make the destructor private. What is the use of private destructor? Whenever we want to control the destruction of objects of a class, we make the destructor private.

Why is my destructor being called?

Destructors are called when one of the following events occurs: A local (automatic) object with block scope goes out of scope. An object allocated using the new operator is explicitly deallocated using delete . The lifetime of a temporary object ends.

How are destructors called in C++?

A destructor is called for a class object when that object passes out of scope or is explicitly deleted. A destructor is a member function with the same name as its class prefixed by a ~ (tilde). For example: class X { public: // Constructor for class X X(); // Destructor for class X ~X(); };

Are destructors automatically called?

Destructor function is automatically invoked when the objects are destroyed. It cannot be declared static or const. The destructor does not have arguments. It has no return type not even void.


1 Answers

Section 3.6.3.2 of the C++03 standard says:

Destructors for initialized objects of static storage duration (declared at block scope or at namespace scope) are called as a result of returning from main and as a result of calling exit.

It doesn't give any restriction regarding having a private destructor, so basically if it gets created it will also get destroyed.

A private destructor does cause a restriction on the ability to declare an object (C++03 12.4.10)

A program is ill-formed if an object of class type or array thereof is declared and the destructor for the class is not accessible at the point of declaration

but since the destructor of A::theMainInstance is accessible at the point of declaration, your example should have no error.

like image 74
Vaughn Cato Avatar answered Oct 19 '22 18:10

Vaughn Cato