Thread: Confused about copy constructor...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    Confused about copy constructor...

    Hi,
    I need help about copy constructor
    Code:
    class A {
    public:
        ...
        A(const A& rhs) {_n1 = rhs._n1; _n2 = rhs._n2;}
    private:
        int _n1;
        int _n2;
    }
    Compiler just gave warning about using private data member of rhs;
    I know that it doesn't need to explicit declare copy constructor in this case.
    But what if I want to declare it explicitly, how should I do? Removing the private access label seems not a good idea for me.
    Thx for reply

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What is the warning?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    here's the code
    Code:
     22    Interval(const Interval& rhs) {
     23       _min = rhs._min;
     24       _max = rhs._max;
     25    }
    
     26    Interval& operator= (const Interval& rhs) {
     27       _min = rhs._min;
     28       _max = rhs._max;
     29       return (*this);
     30    }
    Code:
      2 error: ‘int Interval::_max’ is private
      3 Interval.cpp:25: error: within this context
      4 Interval.h:77: error: ‘int Interval::_min’ is private
      5 Interval.cpp:25: error: within this context

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Can you post a compilable example that demonstrates the problem?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    Well, forget aout above codes.
    I try to use small compilable case to test, and it works.
    Now my question is that.
    If it works, why?
    in copy constructor the arguments const Type& rhs
    why can we directly access member of rhs without using public methods?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Since it works, it's obvious that what you are doing is "correct". The error you are getting stems somehow from something in your code and not from the copy constructor code.
    We can implicitly access private members of an external instance of the same type. The why is probably to be able to write copy constructors.
    Now, if you're doing templated code, you should be aware that if you have a class of type A<T> and try to access members of A<S> inside A<T>, you will get a compile error.
    The solution would be to make A<T> a friend of A<S>.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by ovid View Post
    Well, forget aout above codes.
    I try to use small compilable case to test, and it works.
    Now my question is that.
    If it works, why?
    in copy constructor the arguments const Type& rhs
    why can we directly access member of rhs without using public methods?
    If the code you have supplied is representative of what you are doing, then it works because it is supposed to.

    Access control (private, protected, public) works on a per-class basis: a member function of a class can access the private members of any instance of that class.

    The real question is: what haven't you shown that you did differently, that caused the compilation error?

    My guess - despite the fact you haven't actually informed us - is that your actual example was a template class.

    If X and Y are different classes, and SomeClass is a template class, the member functions of SomeClass<X> are unable to access private members of SomeClass<Y> unless SomeClass<Y> specifically declares SomeClass<X> as a friend.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elysia View Post
    Can you post a compilable example that demonstrates the problem?
    Bad question; his point is that it doesn't compile!

    In any case ovid, how could we possibly know what mistake exists in the real code without it being posted here?
    You either want an answer and you post the relevant real code, or you don't want an answer and you just jerk people around playing guessing games forever. Your choice.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by iMalc View Post
    Bad question; his point is that it doesn't compile!
    The originally supplied code compiles just fine, as it should.
    So I asked for some real code that demonstrates the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 11-21-2007, 10:38 AM
  2. copy array of classes or explicity call constructor
    By Doodle77 in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 11:57 AM
  3. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 04:50 AM
  4. copy constructor
    By paperbox005 in forum C++ Programming
    Replies: 4
    Last Post: 08-20-2004, 01:43 PM
  5. Copy constructor
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2002, 02:02 PM