Constructor protected

Discussion in 'C++' started by away, Sep 22, 2004.

  1. away

    away Guest

    Why some classes are seen to has their constructors declared as "protected"?
    Thanks!
    away, Sep 22, 2004
    #1
    1. Advertising

  2. "away" <> wrote in message
    news:AF74d.618728$...
    > Why some classes are seen to has their constructors declared as
    > "protected"?
    > Thanks!
    >


    Because the constructor is meant to be used by a derived class only.

    If all the constructors are protected then I guess its a way of saying don't
    declare a variable with this class, derive your own class from this class
    and use that. But I'd also say there a better ways of doing that, just
    making the class abstract will also have the same effect.

    john
    John Harrison, Sep 22, 2004
    #2
    1. Advertising

  3. "John Harrison" <> escribió en el mensaje
    news:...
    >
    > "away" <> wrote in message
    > news:AF74d.618728$...
    > > Why some classes are seen to has their constructors declared as
    > > "protected"?
    > > Thanks!
    > >

    >
    > Because the constructor is meant to be used by a derived class only.
    >
    > If all the constructors are protected then I guess its a way of saying

    don't
    > declare a variable with this class, derive your own class from this class
    > and use that. But I'd also say there a better ways of doing that, just
    > making the class abstract will also have the same effect.
    >
    > john
    >


    An abstract class should not include any public constructor, just because it
    is not allowed to create instances directly from it. This is true for
    abstract classes including pure virtual member functions, and for abstract
    classes with all their methods implemented, too.

    But an abstract class should still include at least one protected
    constructor (and, optionally, additional protected and or private
    constructors). The reason is that a class, abstract or not, having data
    members or not, always have the responsibility of initializing itself.

    In a correct object-oriented scheme, all class' data members should be
    declared as private. Thus, a class always should have access to its own
    (private) data members, and any more. In fact, deerived classes should not
    know nothing about their base classes' data members. Consequently, derived
    classes should not have access to their base classes' data members, but
    should access them through their base classes' public and/or protected
    interface. So protected constructors are required by derived classes to
    initialize their base classes in a coherent way.
    Rubén Campos, Sep 22, 2004
    #3
  4. away

    Rolf Magnus Guest

    Rubén Campos wrote:

    > An abstract class should not include any public constructor, just because
    > it is not allowed to create instances directly from it.


    Why?

    > This is true for abstract classes including pure virtual member functions,


    Which would be the definition of "abstract class".

    > and for abstract classes with all their methods implemented, too.


    Implementing a function an making it pure virtual are not mutually
    exclusive.

    > But an abstract class should still include at least one protected
    > constructor (and, optionally, additional protected and or private
    > constructors). The reason is that a class, abstract or not, having data
    > members or not, always have the responsibility of initializing itself.


    If it doens't have members, there is nothing to initialize. Also, if you
    don't write an own constructor, the compiler will generate a default
    constructor for you. There is nothing wrong about using that if it does
    what you need.

    > In a correct object-oriented scheme, all class' data members should be
    > declared as private. Thus, a class always should have access to its own
    > (private) data members, and any more. In fact, deerived classes should not
    > know nothing about their base classes' data members.


    I guess you mean "...should not know anything...". Anyway, there is often
    some knowledge needed, and in many cases, you can use knowledge about the
    internals of a class to get a significant speed optimization.

    > Consequently, derived classes should not have access to their base
    > classes' data members, but should access them through their base classes'
    > public and/or protected interface.


    Usually yes, but that doesn't have anything to do with the constructor's
    access specification.

    > So protected constructors are required by derived classes to
    > initialize their base classes in a coherent way.


    However, for the derived class, there is no difference whether the base
    class's constructor is public or protected. In the case of an abstract base
    class, there is even no difference at all, since that class can't be
    instantiated directly anyway.
    Rolf Magnus, Sep 22, 2004
    #4
  5. >
    > However, for the derived class, there is no difference whether the base
    > class's constructor is public or protected. In the case of an abstract

    base
    > class, there is even no difference at all, since that class can't be
    > instantiated directly anyway.
    >


    That's my main point, in this context having a protected constructor doesn't
    achieve anything, except maybe confusing a few people like the OP.

    john
    John Harrison, Sep 22, 2004
    #5
  6. away

    Ron Natalie Guest

    "Rubén Campos" <> wrote in message news:cirodg$nhl$...
    >
    > An abstract class should not include any public constructor, just because it
    > is not allowed to create instances directly from it. This is true for
    > abstract classes including pure virtual member functions, and for abstract
    > classes with all their methods implemented, too.


    An abstract class doesn't much care whether the constructor is public or not.
    You can't construct one anyhow.

    > But an abstract class should still include at least one protected
    > constructor (and, optionally, additional protected and or private
    > constructors). The reason is that a class, abstract or not, having data
    > members or not, always have the responsibility of initializing itself.


    Classes always have constructors (whether you define them or not).
    Most abstract uses don't have any data anyhow.

    >
    Ron Natalie, Sep 22, 2004
    #6
  7. away

    Andy Guest

    "John Harrison" <> wrote in message news:<>...
    > "away" <> wrote in message
    > news:AF74d.618728$...
    > > Why some classes are seen to has their constructors declared as
    > > "protected"?
    > > Thanks!
    > >

    >
    > Because the constructor is meant to be used by a derived class only.
    >
    > If all the constructors are protected then I guess its a way of saying don't
    > declare a variable with this class, derive your own class from this class
    > and use that. But I'd also say there a better ways of doing that, just
    > making the class abstract will also have the same effect.
    >


    Protected constructors often mean things other than
    "dont-instantiate-me-but-my-child". That's why it is not replaceable
    by an abstract class.

    You might want to make a singleton object and also make the class
    available for extension. In that case an abstract class cannot do what
    a set of protected constructors can.


    Cheers,
    Andy
    Andy, Sep 22, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    2
    Views:
    536
    Andreas Klemt
    Jul 5, 2003
  2. baumann@pan
    Replies:
    4
    Views:
    2,755
    Rolf Magnus
    Apr 20, 2005
  3. Chetan  Raj

    Protected Constructor

    Chetan Raj, Apr 26, 2005, in forum: C++
    Replies:
    10
    Views:
    10,003
    James Kanze
    May 5, 2005
  4. Replies:
    4
    Views:
    495
    James Kanze
    Oct 23, 2007
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,184
Loading...

Share This Page