June 1, 2012

inejose inejose
Lab Rat
17 posts

[Solved] How to create a class that only can be instantiated from another?

 

Hi,

I would like to create a class ( ClassA ) with the following property:

ClassA only can be instantiated from ClassB.

For example, if another class called ClassC tries to instantiate ClassA, compiler must launch an error:

  1. ClassC::someMethod {
  2.  
  3. ClassA *objectA = new ClassA();
  4.  
  5. }

ERROR: ClassC can’t instantiate ClassA.

It is possible?

I was thinking about private constructor and friend class (ClassA and ClassB in my example), but constructor can’t be private by definition.

Thank you in advance.

[Edit: Moved to C++ Gurus forum, as this is not Qt-related; mlong]

 Signature 

call it version 1.0 psicologo online & autoescuela sevilla

6 replies

June 1, 2012

Zlatomir Zlatomir
Dinosaur Breeder
326 posts

Have you tried the code for what you where thinking?
I don’t know about anything that stops you to code a class with private or protected constructor.

And if you give more details about what are you trying to accomplish, maybe some of us come with more appropriate solutions.

June 1, 2012

task_struct task_struct
Hobby Entomologist
344 posts

Hi,

If you put definition of class A inside private section of class B, it will be visible only inside calss B.

 Signature 

“Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program.”
- Linus Torvalds

June 1, 2012

Tannin Tannin
Lab Rat
13 posts

I agree with Zlatomir: the constructor CAN be private and there are many reasonable use-cases (i.e. singletons)
Just remember to also make the copy constructor private because otherwise it will be auto-generated as public, so class C could then make a copy of an A that was created in B.

June 4, 2012

inejose inejose
Lab Rat
17 posts

Thanks to Zlatomir, task_struct and Tannin, it works!

I read in a C++ manual that constructors cannot be private, so I didn’t try.

Finally, I used private constructors and friends class to achieve my first objective as follow:

Class A definition:

  1. class ClassA {
  2.  
  3. public:
  4.     friend class ClassB;
  5.    
  6. private:
  7.     ClassA();
  8.  
  9.     //(edited) Copy constructor added
  10.     ClassA(const ClassA &classAsource);
  11.  
  12.    
  13. };

Class B definition:

  1. class ClassB
  2. {
  3.  
  4. public:
  5.     ClassB();
  6.  
  7. };

Now, only ClassB can create objects from ClassA class, because only ClassB can access to constructor.

In this way I ensure that nobody will use ClassA on my code except ClassB.

Thank you!

 Signature 

call it version 1.0 psicologo online & autoescuela sevilla

June 4, 2012

mlong mlong
Mad Scientist
1517 posts

Don’t forget what Tannin wrote:

Just remember to also make the copy constructor private because otherwise it will be auto-generated as public, so class C could then make a copy of an A that was created in B.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

June 5, 2012

inejose inejose
Lab Rat
17 posts

mlong wrote:
Don’t forget what Tannin wrote:

Just remember to also make the copy constructor private because otherwise it will be auto-generated as public, so class C could then make a copy of an A that was created in B.

Thanks mlong, I will keep it in mind.

(I have edited my previous code example.)

 Signature 

call it version 1.0 psicologo online & autoescuela sevilla

 
  ‹‹ Qdialog box size      Override "operator <<" for char and wchar_t combined ››

You must log in to post a reply. Not a member yet? Register here!