nithithika
June 16th, 2005, 05:18 AM
If a class contain private constructor. Is it possible to declare an object for that class in derived class or some where else. please explain
|
Click to See Complete Forum and Search --> : Private constructor nithithika June 16th, 2005, 05:18 AM If a class contain private constructor. Is it possible to declare an object for that class in derived class or some where else. please explain Hobson June 16th, 2005, 05:57 AM Not in derived class, but only in object of this class itself and in classes declared as friend Hob Or maybe it can be declared anywhere, but defined (thus created) only in special places? Darka June 16th, 2005, 06:02 AM You may also want to consider making the copy constructor and assignment operator private too. nithithika June 16th, 2005, 07:24 AM [QUOTE=Hobson]Not only in derived class, but also in object of this clas itself and in classes declared as friend means is there is a possiblity of object of the derived class can be able to access the base class private constructor without using friend concept. Please explain Hobson June 16th, 2005, 07:29 AM Not only in derived class, but also in object of this clas itself and in classes declared as friend means is there is a possiblity of object of the derived class can be able to access the base class private constructor without using friend concept. Please explain No, it can't. There is a mistake in my post, which I am immediatelly going to correct. Hob NoHero June 16th, 2005, 08:47 AM When the constructor is private this class might follow the Named Constructor Principle, thus the class wants to keep control over it's instances by created them themself. But the Named Constructor Principle can be extended for a class to be act as base class. Just make the constructor private and you are allowed to use it as a base class. A friend class of the NCP class shall also be able to make an instance of this, thus it can access the private data members via the friend modifier. Best Regards nithithika June 17th, 2005, 01:28 AM When the constructor is private this class might follow the Named Constructor Principle, thus the class wants to keep control over it's instances by created them themself. But the Named Constructor Principle can be extended for a class to be act as base class. Just make the constructor private and you are allowed to use it as a base class. A friend class of the NCP class shall also be able to make an instance of this, thus it can access the private data members via the friend modifier. Best Regards oh, it seems that private constructor can be access only by the friend class. could u please explain the term "Named constructor principle", its the first time i'm hearing this NoHero June 17th, 2005, 03:29 AM Before I go overhead here explaining the Named Constructor Idiom you should better take a look at this (http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.8). Good luck! :wave: SuperKoko June 17th, 2005, 03:45 AM If you want a constructor only accessible to derived classes and also accessible by this class, you should use a protected constructor. NoHero June 17th, 2005, 05:57 AM If you want a constructor only accessible to derived classes and also accessible by this class, you should use a protected constructor. Dito. Everything you said is the ParaShift article above ;) nithithika June 17th, 2005, 06:21 AM Before I go overhead here explaining the Named Constructor Idiom you should better take a look at this (http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.8). Good luck! :wave: But in this link named constructor concept is just like a function returning an object. How it'll become a constructor if we declare this fn as static. I'm not ckear in this. SuperKoko June 17th, 2005, 06:25 AM But in this link named constructor concept is just like a function returning an object. How it'll become a constructor if we declare this fn as static. I'm not ckear in this. It is not a true constructor, but it is just a function that creates and returns an object, so conceptually it is a constructor. The limitation of the named "constructors", is that it can only returns dynamic objects (pointer to objects), but it can also be the interest of these constructors. NoHero June 17th, 2005, 06:34 AM It is not a true constructor, but it is just a function that creates and returns an object, so conceptually it is a constructor. The limitation of the named "constructors", is that it can only returns dynamic objects (pointer to objects), but it can also be the interest of these constructors. Another thing is it can easily call the private/protected constructor of the class thus it "constructs" the new instance. The only limitation is that only this static member can construct it. golanshahar June 17th, 2005, 07:29 AM private contructor in a class is used for creating singlton class! casue when u using singleton u want to have only one instance of the class and making a private constructor prevent creating couple instances of the same class! for more information read the post of Pink Panter http://www.codeguru.com/forum/showthread.php?t=344782 if i helped dont forget to rate :-) Cheers NoHero June 17th, 2005, 09:25 AM A good spot though... So which idiom follows the class you are using? nithithika June 22nd, 2005, 07:21 AM thanks for u all. I had another doubt class sample { int i; private: sample(){int i=30;} friend test; }; class test { public: void fn() { sample s; // object declaration for private constructor class cout<<s.i; } }; void main() { test t; t.fn(); } In the above program, sample class is having private constructor and the object declaration for that class is made in its friend class test. Is there any possible way to declare object for the class sample in Main Ejaz June 22nd, 2005, 07:46 AM First of all, please use the code tags while posting the code, its hard to read the code all aligned one corner. Second, there are some mistakes in your code. sample(){int i=30;} should be sample(){i=30;} and return type of main() should be int rather then void. Anyway, now to your question, as already pointed out by NoHero, it can created as following. #include <iostream> using std::cout; using std::endl; class test; class sample { friend test; public: static sample* Create() { return new sample; } void show () { cout << i << endl; } private: sample() { i = 30; } int i; }; class test { public: void fn() { sample s; // object declaration for private constructor class cout << s.i << endl; } }; int main() { test t; t.fn(); sample *ptr = sample::Create(); ptr->show(); delete ptr; return 0; } nithithika June 23rd, 2005, 06:52 AM Thanks Ejaz, ur explanation is very useful for me. codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |