5. Clause 28: Do not let the public in a class member function returns a reference or pointer private member or iterators

 1 class DoSomething {
 2 private:
 3     char * text;
 4     //...
 5 public:
 6     DoSomething():text(nullptr) {}
 7     //DoSomething(){text=nullptr;}
 8     char *& something() const{
 9         return text;
10     }
11 };

In fact, the above code is compiled pass. Const function because the property was abandoned. We know, const qualified function can not change the members in the function, but it can change pointers and references. Ever since the two are contradictory. If you can do that, we do not allow users to modify a number of private member interfaces kind of gave opportunity to the user to modify,

Such damage to the package and the class is enormous.

Guess you like

Origin www.cnblogs.com/Royzzzzz/p/11922239.html