Initialising private base members in copy constructor

Discussion in 'C++' started by Fraser Ross, Aug 6, 2007.

  1. Fraser Ross

    Fraser Ross Guest

    template<typename T,
    typename CounterPolicy = SimpleReferenceCount,
    typename ObjectPolicy = StandardObjectPolicy>
    class CountingPtr : private CounterPolicy, private ObjectPolicy {
    private:
    // shortcuts:
    typedef CounterPolicy CP;
    typedef ObjectPolicy OP;

    T* object_pointed_to;
    public:

    // copy constructor:
    CountingPtr (CountingPtr<T,CP,OP> const& cp)
    : CP((CP const&)cp), // copy policies
    OP((OP const&)cp) {
    this->attach(cp); // copy pointer and increment counter
    }

    I'm not sure why the casts work. Why wasn't static_cast used instead?
    Since the inheritances are private I'm surprised that casts can be done.
    Some compilers don't appear to even require a cast at all.

    Fraser.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    Fraser Ross, Aug 6, 2007
    #1
    1. Advertising

  2. Fraser Ross wrote:
    > template<typename T,
    > typename CounterPolicy = SimpleReferenceCount,
    > typename ObjectPolicy = StandardObjectPolicy>
    > class CountingPtr : private CounterPolicy, private ObjectPolicy {
    > private:
    > // shortcuts:
    > typedef CounterPolicy CP;
    > typedef ObjectPolicy OP;
    >
    > T* object_pointed_to;
    > public:
    >
    > // copy constructor:
    > CountingPtr (CountingPtr<T,CP,OP> const& cp)
    > : CP((CP const&)cp), // copy policies
    > OP((OP const&)cp) {
    > this->attach(cp); // copy pointer and increment counter
    > }
    >
    > I'm not sure why the casts work. Why wasn't static_cast used instead?


    How are the copy constructors defined in the 'CounterPolicy' and the
    'ObjectPolicy' classes used to instantiate this template? IOW, you did
    not post enough code to answer your question.

    > Since the inheritances are private I'm surprised that casts can be
    > done. Some compilers don't appear to even require a cast at all.


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 6, 2007
    #2
    1. Advertising

  3. Fraser Ross

    Fraser Ross Guest

    "Victor Bazarov"
    > How are the copy constructors defined in the 'CounterPolicy' and the
    > 'ObjectPolicy' classes used to instantiate this template? IOW, you

    did
    > not post enough code to answer your question.


    They are usually small classes that don't define copy constructors.
    There is enough code pasted.

    The subject should have been "Initialising private base classes in copy
    constructor".

    Fraser.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    Fraser Ross, Aug 6, 2007
    #3
  4. Fraser Ross

    Fraser Ross Guest

    Fraser Ross, Aug 6, 2007
    #4
  5. On Aug 6, 10:13 pm, "Fraser Ross" <> wrote:
    > Are pointers/references to derived objects convertible to
    > pointers/references of private base classes that are directly inherited
    > as it says in this article:http://msdn2.microsoft.com/en-us/library/5bw4yh4d(VS.80).aspx ?
    >
    > Fraser.
    >
    > --
    > Posted via a free Usenet account fromhttp://www.teranews.com


    yes they are convertible from inside the derived class, and the
    compiler will provide a deafult copy constructor
    if you didn't declare one. See http://www.parashift.com/c -faq-lite/private-inheritance.html
     
    hurcan solter, Aug 6, 2007
    #5
  6. Fraser Ross wrote:
    > "Victor Bazarov"
    >> How are the copy constructors defined in the 'CounterPolicy' and the
    >> 'ObjectPolicy' classes used to instantiate this template? IOW, you
    >> did not post enough code to answer your question.

    >
    > They are usually small classes that don't define copy constructors.


    Ah. OK.

    > There is enough code pasted.


    I am not going to argue based on a technicality.

    > The subject should have been "Initialising private base classes in
    > copy constructor".


    The conversion works regardless of where it occurs. What you have is
    essentially

    class DerivedClass : public BaseClass1, public BaseClass2 {};

    DerivedClass object;
    DerivedClass const& rderived = object;
    BaseClass1 const & rb1 = (BaseClass1 const &) rderived;
    BaseClass2 const & rb2 = (BaseClass2 const &) rderived;

    in which the casts before the 'derived' in the initialisation of 'rb1'
    and 'rb2' are _absolutely_ unnecessary. The conversion should work
    without them. Have you tried removing them at all?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 6, 2007
    #6
  7. Fraser Ross

    James Kanze Guest

    On Aug 6, 8:18 pm, "Fraser Ross" <> wrote:
    > template<typename T,
    > typename CounterPolicy = SimpleReferenceCount,
    > typename ObjectPolicy = StandardObjectPolicy>
    > class CountingPtr : private CounterPolicy, private ObjectPolicy {
    > private:
    > // shortcuts:
    > typedef CounterPolicy CP;
    > typedef ObjectPolicy OP;


    > T* object_pointed_to;
    > public:


    > // copy constructor:
    > CountingPtr (CountingPtr<T,CP,OP> const& cp)
    > : CP((CP const&)cp), // copy policies
    > OP((OP const&)cp) {
    > this->attach(cp); // copy pointer and increment counter
    > }


    > I'm not sure why the casts work. Why wasn't static_cast used
    > instead?


    Because static_cast doesn't ignore the private. This is the one
    thing that you can only do with a C style cast. Except that
    here...

    > Since the inheritances are private I'm surprised that casts can be done.
    > Some compilers don't appear to even require a cast at all.


    At this point, you're in the context of CountingPtr, so you have
    access to the private bases. No cast is required, and IMHO,
    it's very bad policy to use one. (If you have to, of course,
    static_cast is to be preferred, but in general, the conversion
    to base is one of the foundations of OO programming, to the
    point of being an exception to the rule that implicit
    conversions should be avoided.)

    Outside of CountingPtr, of course, static_cast won't work, but a
    C style cast will. (I don't know why the standard says this.
    Some historical reason, probably.) In general, however, private
    inheritance is (or should be) an implementation detail; outside
    of the class, you should code as if it wasn't there.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Aug 7, 2007
    #7
  8. Fraser Ross

    James Kanze Guest

    On Aug 6, 9:13 pm, "Fraser Ross" <> wrote:
    > Are pointers/references to derived objects convertible to
    > pointers/references of private base classes that are directly inherited
    > as it says in this article:http://msdn2.microsoft.com/en-us/library/5bw4yh4d(VS.80).aspx ?


    I didn't read the article in detail, but it certainly started
    out on the right foot. Access control is orthogonal to
    conversions; the implicit conversion exists, regardless of the
    type of heritage (private or public). Access control determines
    whether you have a right to use it or not; if the heritage is
    private, you only have a right to use the conversion within the
    class itself.

    C style casts ignore access control, so you can violate this
    restriction at will. I can't think of a case where you'd want
    to, however. (And I would never use a C style cast for a
    pointer or reference conversion.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Aug 7, 2007
    #8
    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. John Harrison
    Replies:
    0
    Views:
    493
    John Harrison
    Aug 6, 2003
  2. CoolPint
    Replies:
    8
    Views:
    989
    Jeff Schwab
    Dec 14, 2003
  3. Tim Clacy
    Replies:
    3
    Views:
    592
    Nick Hounsome
    Mar 4, 2004
  4. ali
    Replies:
    4
    Views:
    579
    David Harmon
    Mar 5, 2007
  5. cinsk
    Replies:
    35
    Views:
    2,613
    James Kanze
    Oct 11, 2010
Loading...

Share This Page