Solved

protected or private constructors ....

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

813 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

17 Experts available now in Live!

Get 1:1 Help Now