Solved

protected or private constructors ....

Posted on 2003-10-27
5
397 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 60 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 60 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

Independent Software Vendors: 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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
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.

696 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