What is the purpose to declare constructor as protected?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: What is the purpose to declare constructor as protected?

  1. #1
    Join Date
    Jul 2005
    Posts
    954

    What is the purpose to declare constructor as protected?

    Thanks.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: What is the purpose to declare constructor as protected?

    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2005
    Posts
    954

    Re: What is the purpose to declare constructor as protected?

    Thanks for the link. Let's compare two cases that making constructor protected or making constructor private. They both don't allow users to instantiate the object of the class directly. The difference is that making constructor protected makes derived class be able to access the constructor of parent class. What is the use to allow derived class to be able to access the constructor of parent class? Is there any particular reason behind that? After all, the class is not an abstract class, which means it doesn't necessarily need to be inherited from.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Re: What is the purpose to declare constructor as protected?

    I think we already covered protected constructors sufficiently.

    A class with exclusively private constructors not only couldn't be instantiated by client code, it also couldn't be derived from. The only way to obtain an instance of such a class would be calling a factory method.

    (Well, maybe there's another not-yet-detected logic hole in my argumentation above. I've learned something new about private virtual functions just a few days ago - after more than three decades of programming. So, who would really be surprised? )
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Jul 2005
    Posts
    954

    Re: What is the purpose to declare constructor as protected?

    Quote Originally Posted by Eri523 View Post
    I think we already covered protected constructors sufficiently.

    A class with exclusively private constructors not only couldn't be instantiated by client code, it also couldn't be derived from. The only way to obtain an instance of such a class would be calling a factory method.

    (Well, maybe there's another not-yet-detected logic hole in my argumentation above. I've learned something new about private virtual functions just a few days ago - after more than three decades of programming. So, who would really be surprised? )
    I understand a class with a protected constructor can be derived from. But my question is what is the use to derive from such a class? Thanks.

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,704

    Re: What is the purpose to declare constructor as protected?

    Quote Originally Posted by Eri523 View Post
    (Well, maybe there's another not-yet-detected logic hole in my argumentation above. I've learned something new about private virtual functions just a few days ago - after more than three decades of programming. So, who would really be surprised? )
    So don't keep us all in suspense. Do tell!
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Re: What is the purpose to declare constructor as protected?

    Quote Originally Posted by 2kaud View Post
    So don't keep us all in suspense. Do tell!
    You mean the bit about private virtual functions? Here you go: http://forums.codeguru.com/showthrea...79#post2124079 I just noticed that this was in another thread initiated by Larry, BTW.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,347

    Re: What is the purpose to declare constructor as protected?

    Yeah, LarryChen has this habit of asking weird questions, but sometimes they lead to interesting stuff

    By the way, if you want to fill possible gaps in your C++ knowledge such as the one you had concerning private virtual functions (and the non-virtual interface idiom/Template Method pattern), read the Effective C++ series by Scott Meyers, C++ Common Knowledge by Stephen Dewhurst, and the online Guru of the Week (GotW) by Herb Sutter. Some of what they talk about may be outdated with the advent of C++11, but they do contain lots of good stuff.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Re: What is the purpose to declare constructor as protected?

    Thanks for your recommendations, laserlight.

    I've already read many good things about Effective C++, yet didn't perceive it as a series. But well, at lesst seeing it in conjunction with Effective STL, it certainly is one.

    And I just bookmarked GotW, probably will be doing some leisure browsing around the site the next days.

    As I said, I already was aware of the NVI idiom as such, but it may be a good idea to brush up my fundamentals regarding its typical use cases.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  10. #10
    Join Date
    Jul 2005
    Posts
    954

    Re: What is the purpose to declare constructor as protected?

    Quote Originally Posted by laserlight View Post
    Yeah, LarryChen has this habit of asking weird questions, but sometimes they lead to interesting stuff

    By the way, if you want to fill possible gaps in your C++ knowledge such as the one you had concerning private virtual functions (and the non-virtual interface idiom/Template Method pattern), read the Effective C++ series by Scott Meyers, C++ Common Knowledge by Stephen Dewhurst, and the online Guru of the Week (GotW) by Herb Sutter. Some of what they talk about may be outdated with the advent of C++11, but they do contain lots of good stuff.
    Thanks for the recommendations. But it seems nobody answered my question, that is, a class with a protected constructor can be derived from, but what is the use to derive from such a class? In other words, what is the scenario that we design a class which doesn't allow the client to instantiate it and at the mean time allows to be derived from? Thanks.

  11. #11
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Re: What is the purpose to declare constructor as protected?

    Quote Originally Posted by LarryChen View Post
    [...] But it seems nobody answered my question, that is, a class with a protected constructor can be derived from, but what is the use to derive from such a class? In other words, what is the scenario that we design a class which doesn't allow the client to instantiate it and at the mean time allows to be derived from? Thanks.
    I suppose you do know what an abstract class is and what it's good for. A class with only protected constructors effectively is much the same, it just doesn't demand pure virtual functions to manifest abstractness. This is somewhat a C++-specific thing; some other languages have an abstract keyword to disable instantiation and that's it, regardless of constructor visibility.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  12. #12
    Join Date
    Jul 2005
    Posts
    954

    Re: What is the purpose to declare constructor as protected?

    Quote Originally Posted by Eri523 View Post
    I suppose you do know what an abstract class is and what it's good for. A class with only protected constructors effectively is much the same, it just doesn't demand pure virtual functions to manifest abstractness. This is somewhat a C++-specific thing; some other languages have an abstract keyword to disable instantiation and that's it, regardless of constructor visibility.
    Are you saying a class with a protected constructor serves as an abstract class? Think about a singleton class with a protected constructor. You can still call a static member function to create an instance. So obviously a class with a protected constructor is NOT same as an abstract class. Thanks.

  13. #13
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Re: What is the purpose to declare constructor as protected?

    Yeah, right, it wouln't be strictly an abstract class. Instances of it may still be obtained using factory methods, which is a common way to get the singleton instance, but using a factory method isn't tied to the singleton pattern. (Strictly speaking, a singleton instance accessor isn't even a real factory method: It only really produces something once, and then keeps on handing out references to that very item.)

    OTOH non-(really-)abstract classes may have derivates directly (i.e. without the need to use a factory) constructible by client code. There's no compelling need for that, however.

    Factory methods are, BTW, a common way to obtain instances of truely abstract classes. Actually, of course, these are instances of derivates of the abstract class, but this is factually both invisible and irrelevant to client code; it's implementation detail. Quite frequently the actual derived class isn't even known to client code.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  14. #14
    Join Date
    Jul 2005
    Posts
    954

    Re: What is the purpose to declare constructor as protected?

    Quote Originally Posted by Eri523 View Post
    Yeah, right, it wouln't be strictly an abstract class. Instances of it may still be obtained using factory methods, which is a common way to get the singleton instance, but using a factory method isn't tied to the singleton pattern. (Strictly speaking, a singleton instance accessor isn't even a real factory method: It only really produces something once, and then keeps on handing out references to that very item.)

    OTOH non-(really-)abstract classes may have derivates directly (i.e. without the need to use a factory) constructible by client code. There's no compelling need for that, however.

    Factory methods are, BTW, a common way to obtain instances of truely abstract classes. Actually, of course, these are instances of derivates of the abstract class, but this is factually both invisible and irrelevant to client code; it's implementation detail. Quite frequently the actual derived class isn't even known to client code.
    Ok, may I ask a related question here? We can define a singleton by defining its constructor protected. Therefore the singleton class can be derived from. Then what is the benefit to allow a singleton class to be derived from? Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center