Solved

C++ creation of objects

Posted on 2000-04-23
7
198 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
7 Comments
 
LVL 3

Expert Comment

by:V_Bapat
Comment Utility
Hi Ankit
Can you be more precise?

Vicky
0
 

Author Comment

by:ankit_p_a
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 5

Expert Comment

by:Wyn
Comment Utility
kishore_joshi ,you put the constructor in private ,how can ?
0
 

Expert Comment

by:kishore_joshi
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now