Access to private data

I see the following code snippet from a C ++ tutorial, but I think it has a problem with access.

Class A has a private data element valand a public member function const A & topval(const A & b) constthat returns a reference to an object that has a larger val. I think the use b.valbelow is wrong, as it accesses personal data from outside, do I understand correctly?

const A & A::topval(const A & b) const
{
   if (**b.val** > val) 
     return b;
   else 
     return *this;
}
+4
source share
1 answer

No, you can access the private variable of an object from a member function of the object's class.

+4
source

All Articles