Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

protected or private constructors ....

Posted on 2003-10-27
5
Medium Priority
?
415 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 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

927 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