CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2001
    Posts
    5

    Private destructor

    Here is a simplified code piece of mine:


    class A
    {
    A()
    {
    cout << "ccc\n";
    }
    ~A()
    {
    }
    public:
    static A *GetA(void)
    {
    static A a;
    return &a;
    }
    };

    int main()
    {
    A *b = A::GetA();
    return 0;
    }

    It doesn't compile compaining
    error C2248: 'A::~A' : cannot access private member declared in class 'A'

    My mind doesn't work any more. Could someone kindly point out my problem? Thanks.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449
    What compiler are you using? This is a confirmed bug in VC++ 6.0 that has been fixed in VC 7.0.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Feb 2003
    Posts
    14
    By default all functions are declared private. You do not have the public decleration until below your constructor and destructor. I am not sure why you would ever want your constructor private, I can understand protected but not private. If you are going to use class A as just a parent class declare the constructor protected, but I think the destructor has to always be public.

  4. #4
    Join Date
    Sep 2002
    Posts
    1,747
    I know, I know, private ctors / dtors are used to implement singleton classes (classes that can have only one instance throughout a program). This is why there is an instance access method (GetA) available.

    The program is probably in error due to a compiler bug, as Paul McKenzie has pointed out. A workaround is to just not have the dtor private on MS6.0 and grep around by hand for its usage to make sure there are no deletes on the instance in any client code before compilation, or if that is not possible, use a different compiler or just live with the possibility of bad client code. It's not nice, but it gets you by.
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by I know, I know
    By default all functions are declared private. You do not have the public decleration until below your constructor and destructor. I am not sure why you would ever want your constructor private, I can understand protected but not private. If you are going to use class A as just a parent class declare the constructor protected, but I think the destructor has to always be public.
    It is a bug in VC 6.0 that has been fixed in V 7.0.

    Also, destructors can be private -- implementations of singleton patterns make use of private destructors.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Sep 2001
    Posts
    5
    Thanks a lot. Unfortunately I'm using VC6.0.
    I'll leave the destructor as public for now. Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured