Solved

protected or private constructors ....

Posted on 2003-10-27
5
386 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
  • 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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. …
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 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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now