Copy constructor revisited
llewelly@xmission.com
llewelly@xmission.com
Fri Apr 23 00:42:00 GMT 2004
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.)
More information about the Gcc
mailing list