Connecting Tech Pros Worldwide Forums | Help | Site Map

Is a private destructor allowed here?

Joost Kraaijeveld
Guest
 
Posts: n/a
#1: Jul 22 '05
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



Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Is a private destructor allowed here?


"Joost Kraaijeveld" <J.Kraaijeveld@Askesis.nl> wrote...[color=blue]
> 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;
> }[/color]

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


Jonathan Turkanis
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Is a private destructor allowed here?


"Joost Kraaijeveld" <J.Kraaijeveld@Askesis.nl> wrote in message
news:TfcUb.35226$po3.30994@amsnews03.chello.com...[color=blue]
> GCC 3.3.1 allows the following code, Borland C++ 6 gives an error. I[/color]
have[color=blue]
> looked in the standard but could not find the answer. Should this[/color]
code[color=blue]
> compile, given that the private destructor (and constructor but[/color]
there[color=blue]
> Borland does not give an error) is called in the public static[/color]
function:[color=blue]
>[/color]

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


Closed Thread