?
Solved

C++ creation of objects

Posted on 2000-04-23
7
Medium Priority
?
204 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 225 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
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.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

771 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