This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Copy constructor revisited


Robert Schweikert <Robert.Schweikert@abaqus.com> writes:

> I have another question about the copy ctor accessibility requirement
> and the changes in 3.4.
> 
> I pretty much understand the requirements and interpretation of the
> Standard w.r.t. private copy ctor usage. As it pertains to the example
> on http://gcc.gnu.org/gcc-3.4/changes.html
> 
> However, what happens with a protected copy ctor and access of that ctor
> by its derived class?
> 
> Here is the example:
> 
> class A
> {
> // Uncomment the next line to make the error go away
> //    friend class B;
> protected:

When accessing protected members, the access must be through a pointer
    to, reference to, or object of the derived class itself, or any
    class derived from the derived class. (See 11.5). 

Class B derived from A can only access these protected members through 
    instances of B, *not* through instances of A. You can find the
    rational in D&E, 13.9 .

>     A() {}
> 
>     // Copy ctor is protected
>     A(const A& rhs) {}
> 
>     A getMe() const;

getMe() returns an A, by value. Returning an A by value requires an
    accessible copy constructor.

> };
> 
> 
> class B : public A
> {
> public:
>     B():A() {}
> 
>     B(const A& rhs):A(rhs) {}
> 
>     B getMe() const {return A::getMe();}

The call to getMe() results in a temporary on its return. The
    temporary is not a subobject of a B; it is of type A. So the
    constructor is accessed through an A, not a B, So B can't
    access the copy constructor, which is protected.

Note that this problem does *not* occur when the B is constructed for
    the by-value return of B::getMe(). It occurs when the A is
    constructed for the by-value return of A::getMe() .

> };
[snip]

Posting this to a C++ forum such as comp.lang.c++.moderated, or
    comp.std.c++ would fetch more explanations.

(This is intended to be a clarification of what Nathan already said,
    but I thought it would be more comprehensible to reply to this
    post.) 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]