Why can I open private members when I return a link from a public member function?

In the code snippet, I can access the private member variable outside the class. Although this should never be done, why is this permitted in this case? Bad practice of getting returned private variable by reference?

#include <iostream> #include <cstdlib> class foo { int x; public: foo(int a):x(a){} int methodOne() { return x; } int& methodTwo() { return x; } }; int main() { foo obj(10); int& x = obj.methodTwo(); x = 20; // With this statement, modifying the state of obj::x std::cout << obj.methodOne(); getchar(); return 0; } 

And regarding this method, what does the return type convey? And also, when should I return a type of this type?

 int& methodTwo() { return x; } 

PS: I apologize if the topic is vague. Can someone change it to content that matters here. Thank you

+8
c ++ function return-type
source share
5 answers

private does not mean that "this memory can only be changed by member functions" - this means that "direct attempts to access this variable will lead to a compilation error." When you display a link to an object, you are effectively exposing the object.

Bad practice of getting returned private variable by reference?

No, it depends on what you want. Things like std::vector<t>::operator[] would be pretty hard to implement if they couldn't return a non const link. If you want to return the link and don’t want clients to be able to modify it, just make it const .

+15
source share

Returning private members as a reference is perfectly acceptable, and the programmer who writes the class is responsible to carefully choose if it is allowed. This link gives an example of when this can be done.

+3
source share

This code:

  int& methodTwo() { return x; } 

means that the function returns a reference to an integer. In the same way as when passing a value by reference to a function, if the return value of methodTwo , the value returned by methodTwo returned. In this case, a field of class x .

In the code you wrote, this means that you allow the private variable x to hide its scope (class field) and be transmitted in the outside world. This, of course, is bad practice (because x can be changed in ways that may violate the foo class, but it is certainly valid.

Remember public / private / protected - this is only compilation time . When your application compiles, private fields are located next to public fields, and there is no protection against modification. The same is true for managed languages ​​such as C # and Java.

As a rule, you avoid returning links because it makes it crazy - it is difficult to understand when constructors / destructors are called. However, returning the link may be faster. If your method returned a structure type that was HUGE, then returning a const reference to the same structure type should only take four to eight-bytes (a pointer to this object). However, there are better optimization methods for this kind of thing.

+2
source share

As Donotalo said, this is absolutely true. The idea of ​​having private members is to deny other classes / functions access to the private member of the class without your permission. If you are happy to make a function that allows other classes / functions to access your private members, the compiler has nothing against it :-)

It is usually useful to have a private member and have a get function so that other classes / functions can get the value of the function, but only the class can change it.

0
source share

I can access a private member variable outside the scope of the class

If you reference x in main() , then this is different from x declared in class foo . If you try to access obj.x then the compiler will definitely complain.

Bad practice of getting returned private variable by reference?

There is nothing wrong with getting a private member link. But providing a link to a private member makes it inappropriate. By declaring a variable as a private member, you restrict access to this member to only class methods.

regarding this method, what does the return type convey? And also, when should I return a type of this type?

Not sure which method you have in mind?!?!?!

-one
source share

Source: https://habr.com/ru/post/651126/


All Articles