Is a private destructor allowed here?

  • Thread starter Joost Kraaijeveld
  • Start date
J

Joost Kraaijeveld

GCC 3.3.1 allows the following code, Borland C++ 6 gives an error. I have
looked in the standard but could not find the answer. Should this code
compile, given that the private destructor (and constructor but there
Borland does not give an error) is called in the public static function:

class Object
{
public:
static Object& getObject()
{
static Object o;
return o;
}
private:
Object(){}
~Object(){}
};

int main()
{
Object& o = Object::getObject();
return 0;
}

TIA

Joost
 
V

Victor Bazarov

Joost Kraaijeveld said:
GCC 3.3.1 allows the following code, Borland C++ 6 gives an error. I have
looked in the standard but could not find the answer. Should this code
compile, given that the private destructor (and constructor but there
Borland does not give an error) is called in the public static function:

class Object
{
public:
static Object& getObject()
{
static Object o;
return o;
}
private:
Object(){}
~Object(){}
};

int main()
{
Object& o = Object::getObject();
return 0;
}

Nothing is wrong with the code. Since 'getObject' is a member,
it has access to all members of the class, private constructors
and destructors included.

Victor
 
J

Jonathan Turkanis

Joost Kraaijeveld said:
GCC 3.3.1 allows the following code, Borland C++ 6 gives an error. I have
looked in the standard but could not find the answer. Should this code
compile, given that the private destructor (and constructor but there
Borland does not give an error) is called in the public static function:

When you say Borland C++ 6 do you mean C++ Builder 6 or the new
Borland C++ compiler version 6? If the later, you should report this
to Borland.

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top