| |

VerySource

 Forgot password?
 Register
Search
View: 1427|Reply: 13

Is there any way to force access to private data members

[Copy link]

2

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-29 09:20:01
| Show all posts |Read mode
Make a class, test it,
Test the private data members, but they do not have a Set method,
Want to write some values ​​directly to these private data members for testing,
Is there a way to force a value into it?
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-2 00:00:02
| Show all posts
like
But if you don't assign a function to a private member of a class, how will you use this class in the future? ? ?
Reply

Use magic Report

0

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 Brazil

Post time: 2020-3-2 11:15:01
| Show all posts
firend
Reply

Use magic Report

0

Threads

3

Posts

53.00

Credits

Member

Rank: 2

Credits
53.00

 China

Post time: 2020-3-2 11:41:05
| Show all posts
Friends can provide a mechanism to access private members (private data members and private methods), but you can also try to access by pointer offset. This way requires knowing the memory layout of the class, otherwise it is prone to errors.
Reply

Use magic Report

0

Threads

22

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-3-3 17:45:01
| Show all posts
C ++ allows friends to break encapsulation

的 Private members in a class are generally not allowed to be accessed by any function outside the class, but a friend can break this ban and it can access private members of the class (including data members and member functions). Friends can be functions defined outside the class, or the entire class defined outside the class. The former is called a friend function and the latter is called a friend class. Friends break the encapsulation of the class, it is another important object-oriented product of C ++.
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-4 11:45:01
| Show all posts
firend
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-4 22:15:01
| Show all posts
friend
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 Great Britain

Post time: 2020-3-5 12:30:01
| Show all posts
Friends can
But adding a friend is better than adding a set method
Reply

Use magic Report

2

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-3-17 10:30:01
| Show all posts
Don't want to use friend because I don't want to modify the original file
Is there any other way?
Reply

Use magic Report

0

Threads

8

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-3-20 00:00:02
| Show all posts
Rewrite the compiler yourself
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