-4

why in copy constructor we can use rect.p, in main() we can't?

class Rect
{
public:
    Rect()
    {
        p = new int(100);
    }
    Rect(const Rect& r)
    {
        width = r.width;
        height = r.height;
        p = new int(100);
        *p = *(r.p); // OK
    }
    ~Rect()
    {
        assert(p != NULL);
        delete p;
    }
private:
    int width;
    int height;
    int *p;
};
int main()
{
    Rect rect1;
    rect1.p = new int(200); // error
    Rect rect2(rect1);
    return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
Yu Haibo
  • 41
  • 5
  • 1
    You wrote `private:` over the data members including `p`. What do you think `private:` means? – David G Dec 25 '17 at 02:12
  • Recommended reading: [What is the Rule of Three?](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – user4581301 Dec 25 '17 at 02:18
  • The given error - the message of which *should* be included in such questions - does not appear in nor is related to a "copy constructor". Ensure that error descriptions are precise to reduce the chance of a barrage of downvotes.. – user2864740 Dec 25 '17 at 02:20

3 Answers3

1

Because all members of a class are friends which each other. Private data members can only be modified by members of the class or friend classes.

When you try to access rect.p in main(), you're trying to access private data members from a public interface.

EDIT: fixed a wording error

Gabriel
  • 829
  • 5
  • 12
1

p is a private member of your class, therefore only a method of your class can access and/or modify it.

Make some research about getters and setters if you want to know more about it.

Leop
  • 75
  • 7
  • A `friend` can also access `private` members. – user4581301 Dec 25 '17 at 02:15
  • Sure but the author doesn't seem to know much about classes, so I just explained the minimum he has to know to understand his mistake and gave him a link to help him. – Leop Dec 25 '17 at 02:16
  • Here rect1 is a class obj, i understand use *p is OK But r.p i have trouble,because r is a refrence of rect1,so i don't understand – Yu Haibo Dec 25 '17 at 02:31
1

This is a basic question about access right for a class.

Everything you defined in "private": All the members, functions, friend members/functions of the class have access to the stuff in "private" block.

Everything you defined in "public": On the opposite side of "private", everyone have access to the stuff in "public" block. Integer pointer p is defined in "private" block, so it can be used by constructor(because constructor is one part of the class). But main function is "out of" class. So main function have no access of pointer p.

If you want to get the point p in main funcion, you can define a public function "getPointer()" to return pointer p and call "getPointer()" in main function.

Come back to your code.

MA Shengjing
  • 113
  • 1
  • 7
  • set or get Method I understand , usually class obj can't use private member,but in Copy-construct class obj can do – Yu Haibo Dec 25 '17 at 02:39
  • @Yu Haibo, if you want to get member data by obj.member, this member must be "public". Why copy-construtor can access to point p is that copy-construct is "inside" class and he has all the access to private members. – MA Shengjing Dec 25 '17 at 02:52
  • OK,since rect1 has been construct , rect1 has own private member address,but all class obj has comman copy-construct so we can use – Yu Haibo Dec 25 '17 at 03:21