| |

VerySource

 Forgot password?
 Register
Search
View: 1598|Reply: 10

Why can the private members of the actual parameters be directly accessed in the copy constructor?

[Copy link]

2

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-11-10 12:00:02
| Show all posts |Read mode
Why can you directly access the private members of the actual parameters in the copy constructor?
Such as:
class demo
{
        private:
                int i;
        public:
                demo();
                demo(const demo&);
};

demo::demo(const demo&obj)
{
        i = obj.i; "Private members are used directly here!
}

This can be compiled, why?
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-11-10 12:45:01
| Show all posts
To distinguish that a member function of a class can access any member of the class, not just an object
Your own member function can access any of your own members, through the member function of each object you can access the object
Any member of other objects of the same type.
Reply

Use magic Report

0

Threads

14

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-11-10 13:45:01
| Show all posts
Yes, access permissions are for the class, not for the object.
Reply

Use magic Report

0

Threads

20

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-11-10 14:15:01
| Show all posts
A member function of a class can access any member of the class, so it must be able to access the private variable int i in the class in this file.
Reply

Use magic Report

0

Threads

41

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-11-10 14:30:01
| Show all posts
I am my friend function
Reply

Use magic Report

0

Threads

12

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-11-10 15:00:01
| Show all posts
There's nothing more to say, grasping the foundation is the right way
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-11-10 15:15:01
| Show all posts
Because the member function has a hidden this pointer to point to the main object
such as
class A
{
public:
    void ok(int i){a = i;}
private:
    int a;
}

int main()
{
    A a;
    a.ok(3);
    return 0;
}
The compiler will parse a.ok() like this (just a model)
ok(int i, A *this)
{
    this -> a = i;
}
That's what I mean
{
}
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-11-10 15:30:01
| Show all posts
i = obj.i
======
The obj.i on the right can be used directly
Is determined by the process of class domain name resolution,
Inside the member function, when parsing the name, first look at the local domain of the function, and then the entire class domain (they can be accessed without distinguishing between public, private, and protected)
The grammatical rules of C++, the compiler implements it like this, just understand
Reply

Use magic Report

0

Threads

37

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-11-10 16:15:01
| Show all posts
hzxszmh
   
I am my friend function

learned
Reply

Use magic Report

0

Threads

7

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-11-10 16:30:01
| Show all posts
This has nothing to do with copy construction, any member function can be, because they are the same class
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list