This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Copy constructor revisited
- From: Robert Schweikert <Robert dot Schweikert at abaqus dot com>
- To: gcc-dev-lst <gcc at gcc dot gnu dot org>
- Cc: robert dot schweikert at abaqus dot com
- Date: 22 Apr 2004 10:59:37 -0400
- Subject: Copy constructor revisited
- Organization: ABAQUS
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:
A() {}
// Copy ctor is protected
A(const A& rhs) {}
A getMe() const;
};
class B : public A
{
public:
B():A() {}
B(const A& rhs):A(rhs) {}
B getMe() const {return A::getMe();}
};
B can access the copy ctor of A since it is protected and not private
and B is a derived class of A. Yet trying to compile this code yields an
error:
g++ -c tryIt.C
tryIt.C: In member function `B B::getMe() const':
tryIt.C:9: error: `A::A(const A&)' is protected
tryIt.C:22: error: within this context
If I add the friend declaration the error goes away. I think this is a
bug. Here is what I think should be happening.
-> call B::getMe
-> call A::getMe
-> return A
-> create B by using the conversion ctor B(const A& rhs):A(rhs) {}
-> access A copy ctor; this should be OK since it is protected and B is
a derivative of A
Help is appreciated. If this is not a bug I'd appreciate if someone
could explain to me why the friend declaration makes a difference here.
If it is a bug, I'll be happy to file it in bugzilla. Any chance to get
a patch against the just released 3.4.0 tree?
Thanks,
Robert
--
Robert Schweikert <Robert.Schweikert@abaqus.com>
ABAQUS