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


1. B::B(const A& rhs)is Not a copy constructor.
   Only things like B::B(B&), B::B(const B&), B::B(volatile B&) are 
   copy constructors. If the compiler does not find one of the above,
   it will generate its own as B::(const B & other) : A(other) {}
 
   Despite of the above, this is not the cause for your failure.

2. As I remember it, derived class B can access protected elements of A 
only
      through B. This means that if you have B::Foo(A&other), you can't 
access
      protected symbols of 'other'.
      On the other side, B::Foo(B&other) can access the protected symbols

      I don't have the time to look this up in ISO C++.





Robert Schweikert <Robert.Schweikert@abaqus.com>
Sent by: gcc-owner@gcc.gnu.org
22/04/2004 17:59
 
        To:     gcc-dev-lst <gcc@gcc.gnu.org>
        cc:     robert.schweikert@abaqus.com
        Subject:        Copy constructor revisited


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



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