Results 1 to 6 of 6

Thread: What is the use of private constructor in C++?

  1. #1
    Join Date
    Nov 2009
    Posts
    67

    What is the use of private constructor in C++?

    Hello to all,
    I am second year Computer science student. I recently started learning c++ language. In our last lecture we learn about private constructor, but I didn't understand this concept clearly. Can anyone tell me what is the use of private constructor in C++? Please help me. Thank you.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: What is the use of private constructor in C++?

    As per my information private constructor is used to prevent your class object from being created in another place, other than this very class. You can use lots of use cases to provide such type of constructor. Using this private constructor your class object is created in a static method and after that your static method is defined as public.
    Code:
    class MyClassEg()
    {
    private:
      MyClassEg() { }
    
    public:
      static MyClassEg * CreateInstanceEg() { return new MyClassEg(); }
    };

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: What is the use of private constructor in C++?

    Private constructor is used in c++ to create object of class only one time. It means that your class does not have more that one instance of your class. I have written following code for you. Just try to understand it.
    Code:
    class MyClassEg()
    {
    private:
      MyClassEg() { }
    
    public:
      MyClassEg & Instances()
      {
        static MyClassEg * aGlobalInsts = new MyClassEg();
        return *MyClassEg;
      }
    };

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: What is the use of private constructor in C++?

    private constructor is same as any other constructor but it is declared as private. Due to this you can only access object of that class in only that class and not in other class. Most of the people use private constructors which do all the work than public constructor. You can use following code to understand the concept.
    Code:
    class MyClassEgs
    {
    public:
      MyClassEgs() : MyClassEgs(2010, 1, 1) { }
    
    private:
      MyClassEgs(int theYears, int theMonths, int theDays) { /*code do real work */ }
    };

  5. #5
    Join Date
    Jan 2008
    Posts
    1,521

    Re: What is the use of private constructor in C++?

    I think private constructor is used to prevent from object copying. It is very easy to use and implement. You have to just use Private access specifier before your constructor. When you use private constructor in your code, then it means that you are prevnting your class object from being used in another place.
    Code:
    class MyClass1
    {
      SharedsResources * myResources;
    
    private:
      MyClass1(const MyClass1 & s) { }
    };

  6. #6
    Join Date
    May 2008
    Posts
    2,012

    Re: What is the use of private constructor in C++?

    If you want singletons in your code then in this case you have to declare all your constructors as private to do this. It is very simple do this. Just use following code to understand the concept of private constructor.
    Code:
     
          typedef auto_ptrs<Tests> TestPtrs;
      
          TestPtrs Tests::creates() throw(bads_allocs)
      
          {
      
          return new Tests();
       
          }

Similar Threads

  1. What is constructor overloading in c++
    By Mast Maula in forum Software Development
    Replies: 4
    Last Post: 08-01-2011, 10:34 AM
  2. copy constructor Vs constructor
    By Truster in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 02:49 PM
  3. Selecting a constructor to use with new in C++
    By Carlton in forum Software Development
    Replies: 5
    Last Post: 02-02-2010, 12:30 AM
  4. use of methods and constructor
    By beelow in forum Software Development
    Replies: 4
    Last Post: 14-11-2009, 01:37 AM
  5. Constructor in Java
    By Dharamsi in forum Software Development
    Replies: 4
    Last Post: 06-03-2009, 12:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,727,103,030.98636 seconds with 17 queries