Can we have private constructors and destructors

Discussion in 'C++' started by Rajesh Garg, Jul 22, 2003.

  1. Rajesh Garg

    Rajesh Garg Guest

    Can we have private constructors and destructors? IF yes what is the
    use of such constructors or destructors.....in the sense where can
    these be implemented in a system.................

    I have an idea that we can have private constructors and destructors
    but am not able to find a situation where they can be used...

    Regards
    RVG
    Rajesh Garg, Jul 22, 2003
    #1
    1. Advertising

  2. Rajesh Garg

    Maik Schmidt Guest

    Rajesh Garg wrote:
    > Can we have private constructors and destructors? IF yes what is the
    > use of such constructors or destructors.....in the sense where can
    > these be implemented in a system.................
    >
    > I have an idea that we can have private constructors and destructors
    > but am not able to find a situation where they can be used...


    Typically you declare methods private to prevent others from calling
    them. If you declare a constructor private, you prevent others from
    creating objects of your class from the outside. This makes sense under
    some circumstances, e.g. if you implement the so called singleton
    pattern. This pattern makes sure, that only one object of a certain
    class exists, so you have to control explicitly, who creates that one
    and only object.

    Cheers,

    <maik/>
    Maik Schmidt, Jul 22, 2003
    #2
    1. Advertising

  3. Rajesh Garg

    David White Guest

    Rajesh Garg <> wrote in message
    news:...
    > Can we have private constructors and destructors?


    Yes, in fact in a class they are private by default if you don't specify the
    access rights.

    > IF yes what is the
    > use of such constructors or destructors.....in the sense where can
    > these be implemented in a system.................


    You might use them if there is allowed to be only one instance of a class
    (called a singleton). You allow a single function to access the constructor
    (either a friend or a public static member function), and it will only
    create an instance if it hasn't already done so. You don't want just anyone
    to be able to delete that one instance, so you could make the destructor
    private as well.

    DW
    David White, Jul 22, 2003
    #3
  4. "Tamer Higazi" <> wrote...
    > As long as i know are constructors and destructors only provided in an
    > PUBLIC Area to give parameters for a class.
    >
    > All constructors and destructors MUST be declarated "public" to get
    > access to it.


    Nonsense. Just like other members of the class, constructors and
    destructors can have access specifiers which limit the places where
    objects of that class can be created or destroyed. "Private" access
    specifier limits those places to either other member functions of
    the same class or friends (classes or functions).

    > The private area are class variables and methods which can be called
    > from public methods only.


    No. They can be just as well called from private member functions
    or from functions declared as friends or from member functions of
    classes declared as friends.

    > But you won't be able to call these methods
    > and variables outside the class in the "main" part.


    Yes, that's the main idea.

    > Konstruktors and Destruktors are only good if you want to give special
    > values on the way to a class. Even you can overload them, by having
    > different numbers (kinds) of parameters.


    Nonsense. Constructors are needed if you want to have the control
    over the process of constructing of the object (opening files,
    checking system conditions, etc.)

    > the destructor destroys the konstructor and frees the memorie.


    This statement doesn't make sense.

    > you are NOT forced to create an Konstruktor and Destruktor. If you don't
    > declare them, then a Standard Kon- and destruc- tor will be called


    Not "standard". "Default".

    > I make you an example:
    >
    > class GoodTest
    > {
    >
    > private:
    > int result;
    >
    > public:
    >
    > GoodTest(int a, int b, int c)
    > {
    > result = a * b *c;
    > }
    >
    > ~GoodTest()
    > {
    > };
    >
    > void output
    > {
    > cout << "result is: " << result;
    > }
    >
    > };
    >
    > int main()
    > {
    >
    > GoodTest Teste(3,4,5);
    > Teste.output();
    > return 0;
    > }


    Along with the fact that this example doesn't compile due to errors,
    it also doesn't demonstrate the use of _private_ constructors or
    destructors as the OP asked.

    >
    >
    >
    > Rajesh Garg wrote:
    > > Can we have private constructors and destructors? IF yes what is the
    > > use of such constructors or destructors.....in the sense where can
    > > these be implemented in a system.................
    > >
    > > I have an idea that we can have private constructors and destructors
    > > but am not able to find a situation where they can be used...


    Polymorphic objects. Constructed by a "factory method" or a friend
    function, the objects reside in freestore. The user receives
    a pointer to the object and uses it, then passes it to be destroyed
    in another function.

    class SomeBaseClass {
    SomeBaseClass(); // private constructor
    virtual ~SomeBaseClass(); // private destructor
    public:
    static SomeBaseClass* createMeOne(int); // loaded from a DLL
    virtual void doWork();
    static void deleteThis(SomeBaseClass*&);
    };

    int main() {
    SomeBaseClass* p = SomeBaseClass::createMeOne(42);
    p->doWork();
    SomeBaseClass::deleteThis(p);
    }

    ... somewhere in the library

    class SomeDerivedClass : public SomeBaseClass {
    public:
    SomeDerivedClass(int);
    private:
    void doWork() {
    doActualWork();
    }
    };

    SomeBaseClass* SomeBaseClass::createMeOne(int what) {
    return new SomeDerivedClass(what); /// <<<<<<<<<<<<<<<<< !!!
    }

    void SomeBaseClass::deleteThis(SomeBaseClass*& ptr) {
    delete ptr;
    ptr = 0;
    }

    Victor
    Victor Bazarov, Jul 22, 2003
    #4
    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. sieg1974
    Replies:
    4
    Views:
    596
    jeffc
    Apr 23, 2004
  2. Ruben Campos
    Replies:
    5
    Views:
    4,169
    Ron Natalie
    Apr 7, 2005
  3. Replies:
    3
    Views:
    308
    Chris Theis
    Aug 5, 2005
  4. Alok
    Replies:
    5
    Views:
    4,803
    ryadav
    Apr 16, 2006
  5. Peng Yu
    Replies:
    5
    Views:
    394
    Juha Nieminen
    Sep 19, 2008
Loading...

Share This Page