Private constructor

Discussion in 'C Programming' started by lxman, Jun 3, 2010.

  1. lxman

    lxman

    Joined:
    Jun 3, 2010
    Messages:
    1
    Maybe I am misunderstanding. I hope you folks can set me straight.

    I understand that I can declare a private constructor in a class. I do this at times in order to simply provide the member functions for the class. But now I am trying to declare a descendant of this class with the private constructor and the compiler is complaining.

    class foo {
    private:
    foo(){}
    ~foo(){}
    };

    class foo2: public foo {
    public:
    foo2(){}
    ~foo2(){}
    };

    I would like to prevent someone from creating a foo2 object from being able to call the constructor/destructor directly, but the compiler complains that foo(){} is private.

    Is there a different way to prevent access to the constructor/destructor from foo2, or am I just practicing bad design here?
    lxman, Jun 3, 2010
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    19
    Views:
    772
    Daniel T.
    Feb 4, 2004
  2. DaveLessnau
    Replies:
    3
    Views:
    413
    Howard
    May 16, 2005
  3. Jeremy Bowers
    Replies:
    3
    Views:
    501
    Steven Bethard
    Jan 24, 2005
  4. plmanikandan@gmail.com

    Private constructor,Static constructor

    plmanikandan@gmail.com, Mar 3, 2006, in forum: C++
    Replies:
    4
    Views:
    4,109
    Alf P. Steinbach
    Mar 3, 2006
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,191
    robertwessel2@yahoo.com
    Nov 28, 2007
Loading...

Share This Page