Solved

C++ creation of objects

Posted on 2000-04-23
7
202 Views
Last Modified: 2010-04-02
How I can create only one object of a class ?It should be only one object for it.
0
Comment
Question by:ankit_p_a
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 3

Expert Comment

by:V_Bapat
ID: 2742989
Hi Ankit
Can you be more precise?

Vicky
0
 

Author Comment

by:ankit_p_a
ID: 2742995
I have a class myclass with one data member.Now I want to create only one object of myclass.I must not be allowed to create more than one object.If I try to do so it should give message i.e
error,by saying it is not possible
0
 

Accepted Solution

by:
kishore_joshi earned 75 total points
ID: 2743000
Hai ,
Here class A is called as a Singleton class. The ristriction is placed that ONLY ONE object of that class can be created.


class A
{
private:
static int count;

A()
{
   count++;
}

public:

static A* CreateObj()
{
     A *a = new A;
    if(count > 1)
    {
      cout<<" Crossed the limit........ ";
      return NULL;

    }
   else
    {
      return a;
    }

  }

};


int A::count;

void main()
{

      A *r1,*r2 ,*r3;

      r1 = A::CreateObj();
      r2 = A::CreateObj();
      r3 = A::CreateObj();

}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:Wyn
ID: 2743183
kishore_joshi ,you put the constructor in private ,how can ?
0
 

Expert Comment

by:kishore_joshi
ID: 2743236
Wyn,

Only if u put the constructor in the private, u can force the user to create the object by calling that static function CreateObj. Here in this function, we can check the value of static variable "count" and thus keep a control on the number of objects getting created.



0
 
LVL 5

Expert Comment

by:Wyn
ID: 2743813
kishore_joshi ,if you define constructor explicitly in private part.Aint this compiler error ?
The compiler cannot call constructor when we use new to create objects...
Regards
Eirnava...
0
 

Expert Comment

by:kishore_joshi
ID: 2746116
Hi Eirnava,
Its NOT compiler error. By declaring constructor in the private, we are forcing
the user to create the object only thru a static function.

If u declare the Copy Costructor (CC) also in the private, then u place another restriction on the user .....
i.e. u cannot pass the object of that class by value to any function, u HAVE to pass the object ONLY by reference.

Please try and execute the example that I have mentioned in the answer.
It should be more clear.  

Bye,
Kishore
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question