Solved

C++ creation of objects

Posted on 2000-04-23
7
203 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

691 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