W jaki sposób są wywoływane prywatne destruktory obiektów statycznych? [duplikować]
Możliwy duplikat:
Nie można uzyskać dostępu do elementu prywatnego w destruktorze klasy singleton
Implementuję singleton jak poniżej.
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;
}
Thedestruktor jest prywatny. Czy to zostanie wywołane dla obiektu MainInstance, gdy program ma się zakończyć?
Próbowałem tego w Visual Studio 6, dał błąd kompilacji.
"cannot access private member declared in class..."
W visual studio 2010 to zostało skompilowane iwywołano destruktor.
Jakie powinny być oczekiwania zgodnie ze standardem?
Edytuj: zamieszanie powstaje, ponieważ zachowanie Visual Studio 6 nie jest tak głupie. Można argumentować, że konstruktor A dla obiektu statycznego jest wywoływany w kontekście funkcji A. Aledestruktor nie jest wywoływany w kontekście tej samej funkcji. Nazywa się to z kontekstu globalnego.