Purpose of protected constructors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • parag_paul@hotmail.com

    Purpose of protected constructors

    I saw an implementaiton with protected constrcutors.
    What could be use of this construct be?
  • Noah Roberts

    #2
    Re: Purpose of protected constructors

    parag_paul@hotm ail.com wrote:
    I saw an implementaiton with protected constrcutors.
    What could be use of this construct be?
    The purpose would be to keep clients from instantiating the base but
    still allowing it to be subclassed. I would gather that the default
    constructor is not sufficient to build the base and that a "custom"
    constructor needs to be called by the subclass during its own construction.

    There are numerous reasons why you would not want to allow direct
    instantiation of a class or base.

    Comment

    • Andrey Tarasevich

      #3
      Re: Purpose of protected constructors

      parag_paul@hotm ail.com wrote:
      I saw an implementaiton with protected constrcutors.
      What could be use of this construct be?
      Protected constructor is one possible way to implement the OOP concept
      of "abstract base class" in C++. Protected constructor indicates that
      the class is not supposed to be instantiated as a standalone object, but
      only as a base class subobject of another class. A classic example would
      be 'std::basic_str eambuf' class [template] from the standard library.

      Of course, the "abstractne ss" of a class can be reflected in C++ code by
      other means, like introduction of at least one pure virtual method. A
      protected constructor is just an alternative approach.

      --
      Best regards,
      Andrey Tarasevich

      Comment

      • R.A. Nagy

        #4
        Re: Purpose of protected constructors


        <parag_paul@hot mail.comwrote in message
        news:1a59f44d-7e5d-4517-85c5-44d4e052343a@j1 g2000prb.google groups.com...
        >I saw an implementaiton with protected constrcutors.
        What could be use of this construct be?
        Another purpose of a protected *or private* constructor would be to allow a
        static member function to instance it's own class.

        It is a very handy pattern. One that allows a single class to serve a both a
        model AND it's own instance-controller.

        Java uses the paradigm a lot.

        R.A. Nagy




        Comment

        • Test Name

          #5
          Re: Purpose of protected constructors

          "Andrey Tarasevich"
          Protected constructor is one possible way to implement the OOP concept
          of "abstract base class" in C++. Protected constructor indicates that
          the class is not supposed to be instantiated as a standalone object,
          but
          only as a base class subobject of another class.
          There are some loopholes to that method. Are there any experts that
          have recommended this?

          Fraser.


          Comment

          • Juha Nieminen

            #6
            Re: Purpose of protected constructors

            Noah Roberts wrote:
            There are numerous reasons why you would not want to allow direct
            instantiation of a class or base.
            Can you give an example of such situation, where it cannot be done by
            putting a pure virtual function in the base class?

            Comment

            • Noah Roberts

              #7
              Re: Purpose of protected constructors

              Juha Nieminen wrote:
              Noah Roberts wrote:
              >There are numerous reasons why you would not want to allow direct
              >instantiatio n of a class or base.
              >
              Can you give an example of such situation, where it cannot be done by
              putting a pure virtual function in the base class?
              struct Base
              {
              private:
              struct impl;
              impl * pimpl;
              protected:
              Base();
              public:
              boost::shared_p tr<Baseconstruc t_base();

              /* ... functionality - virtuals but no pure */
              };

              struct Derived
              {
              private:
              struct impl; impl * pimpl;
              Derived();
              public:
              boost::shared_p tr<Derivedconst ruct_derived();

              /* behavior overrides */
              };

              Derived.cpp:

              Derived::Derive d() : Base(), pimpl(new impl) {}

              You might consider this idiom any time you want to be sure an object is
              never constructed on the stack and never NOT put in a shared_ptr (such
              as when also subclassing shared_ptr_from _this).

              Another example would be the use of a factory. You want to be sure the
              object is always created through the factory but it also needs to be
              constructed. The abstraction a factory generates is usually a real
              abstract object, but maybe that's not appropriate in all cases. You can
              toss in a pure virtual for no reason other than to not have to protect
              your constructor, but what's the point in that?? Furthermore you'd
              still want to protect construction of derived objects and might wish to
              allow them also to be extended by subclassing.

              Finally, protecting the constructor says something about the object. It
              says that constructing this object through the constructor is not in its
              public interface.

              Those are the cases I can think of on the spot.

              Comment

              • Andrey Tarasevich

                #8
                Re: Purpose of protected constructors

                Test Name wrote:
                "Andrey Tarasevich"
                >Protected constructor is one possible way to implement the OOP concept
                >of "abstract base class" in C++. Protected constructor indicates that
                >the class is not supposed to be instantiated as a standalone object,
                but
                >only as a base class subobject of another class.
                >
                There are some loopholes to that method. Are there any experts that
                have recommended this?
                ...
                Well, apparently there are, since, as I said before, this is how
                abstractness of 'std::basic_str eambuf' base class is implemented in the
                C++ standard library.

                What specific loopholes are you referring to?

                --
                Best regards,
                Andrey Tarasevich

                Comment

                • Andrey Tarasevich

                  #9
                  Re: Purpose of protected constructors

                  Fraser Ross wrote:
                  ...
                  As someones said a static member function could invoke a constructor.
                  Also a friend could. Possibly a nested class also.
                  So what? Just like with any C++ feature, whether it offers any
                  protection from malicious intent is secondary or irrelevant. Protected
                  constructor only _expresses_ the purpose of the class. Once you are
                  aware of that purpose, it is up to you to decide to use it properly or
                  improperly.
                  The standard does not describe an ABC as you have.
                  I explicitly stated in my original message that I'm talking about the
                  concept of ABC in the general OOP sense of the term, not in strict C++
                  sense. C++ ABC is a way to implement OOP ABC, which does not mean that
                  this is the only way to implement the OOP ABC. Moreover, OOP ABC is just
                  a design concept that does not require to be implemented/enforced at the
                  language level at all. From the OOP point of view, a class in an ABC if
                  I said it is ABC. The opportunity to _enforce_ that fact to some degree
                  by using some C++ feature (like a protected constructor or a pure
                  virtual method) is just an optional icing on the cake.

                  Also, you apparently failed to notice that the standard itself refers to
                  'std::basic_str eambuf' as an ABC, while in fact it does not satisfy
                  the standard C++ definition if ABC at all :)

                  --
                  Best regards,
                  Andrey Tarasevich

                  Comment

                  Working...