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
Solved

C++ creation of objects

Posted on 2000-04-23
7
200 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
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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 128
Best book to learn C++ 4 84
operator overload: incompatible type and too many params 5 83
Add values of each row in an array 3 64
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…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

828 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