Connecting Tech Pros Worldwide Help | Site Map

Is a private destructor allowed here?

  #1  
Old July 22nd, 2005, 07:08 AM
Joost Kraaijeveld
Guest
 
Posts: n/a
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


  #2  
Old July 22nd, 2005, 07:08 AM
Victor Bazarov
Guest
 
Posts: n/a

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


  #3  
Old July 22nd, 2005, 07:08 AM
Jonathan Turkanis
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
singleton for a parent with protected destructor yccheok@gmail.com answers 2 September 12th, 2005 04:25 PM
about copy constructor and destructor Tony Johansson answers 3 August 12th, 2005 02:55 AM
Why is the Base class Constructor getting called twice Robert answers 6 July 22nd, 2005 05:11 AM
Exception and derived destructor Miri answers 4 July 19th, 2005 06:03 PM