Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

protected or private constructors ....

Posted on 2003-10-27
5
Medium Priority
?
410 Views
Last Modified: 2010-04-01
Hi C++ Experts,

   Is there any specific reason that we want to make the constructors private of protected ? In either case, how do we create objects of the class ? Is that trut that we can not use "A a ;" or
"new A" to create objects in these two cases ???
   Thanks.
0
Comment
Question by:meow00
[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
  • 3
5 Comments
 
LVL 19

Assisted Solution

by:Dexstar
Dexstar earned 240 total points
ID: 9629657
meow00:

> Is there any specific reason that we want to make the constructors private of
> protected ? In either case, how do we create objects of the class ?

The only reason I can think of to make a constructor private is if you do not want that object created.  For example, if you have a class with ONLY static functions, there is no need to create an instance of that class, so you should make the constructor "private", like this:

     class CTools
     {
     private:
          CTools()     { };  // Keep the class from being created

     // Operations
     public:
         static int DoSomething();
         static int DoSomethingElse();
     };

PROTECTED constructors are useful if you FORCE a class to be a base class, and prevent it from being used directly.

For example:
     class CBase
     {
     protected:
          CBase()     { }
     };

     class CDerived : public CBase
     {
     public:
          CDerived()     {     };
     };

In that case, you wouldn't be able to create an object of type "CBase" because the constructor is protected.  However, you can create an object of type "CDerived" because CDerived is based on CBase, and can access the protected constructor.

Does that make sense?

Hope that helps,
Dex*
0
 
LVL 1

Author Comment

by:meow00
ID: 9629788
I see ....... so is that true we can construct a Base object (using the protected or private constructor) from it's friend class ? Is this something good to do ? or better not do it ?

thanks.
0
 
LVL 19

Accepted Solution

by:
Dexstar earned 240 total points
ID: 9629810
meow00:

> I see ....... so is that true we can construct a Base object (using the
> protected or private constructor) from it's friend class ? Is this something
> good to do ? or better not do it ?

What friend class are you talking about?  In theory, you could make a friend class and that friend class would have access to the protected/private constructors.  But, in general, you shouldn't use "friends", unless there is no other way to do it.

You should only use protected/private constructors if there is a good reason to do so, like in the examples I gave you already.  I can't think of a situation where it would be valuable to use a "friend".

Dex*
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9629852
>> Is there any specific reason that we want to make the constructors private of protected ?

Yes there is. A protected constructor can only be called from within a derived class. This is usefull if you have a base class that you want to inherit from but that you do not want an instances of it to be created explicitly. For example,

#include <iostream>
#include <iomanip>

using namespace std;

class test1
{
    protected:
        test1() { cout << "test1 constructor called." << endl; }
        ~test1() { cout << "test1 destructor called." << endl; }
};

class test2 : test1
{
    public:
        test2() { cout << "test2 constructor called." << endl; }
        ~test2() { cout << "test2 destructor called." << endl; }
};

int main()
{
    test2 t;

    return 0;
}

-- output --
test1 constructor called.
test2 constructor called.
test2 destructor called.
test1 destructor called.

Take notice that the constructor in the base class is called before the constructor of the derived class and that conversly, the derived class' destructor is called before the base class' destructor.

As for private constructors, you can use them to create a singleton class. That is, a class of which there can only be instance of at any one time. This is a rather rare circumstance where you need to have only one instance of the class in existence. You do this by making the constructor private. You then have a static member function that returns a reference to the only instance of the class. If a class instance does not exist, the function creates one.

Cheers!
Exceter
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9629930
Whoa...  Never assisted myself before!  Cool!  :)
0

Featured Post

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!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.

705 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