c++ why doesn't a copy of an object allow access to a private variable of the original? -
c++ why doesn't a copy of an object allow access to a private variable of the original? -
for example,
number operator+(number a, number b) { homecoming number(a.x + b.x); } would cause kind of "unable access private fellow member error". understand if don't pass reference, number a, , number b copied on stack , used in body of function. however, don't see why not allow access originals' private members. how misunderstanding concept of object? how come friend , fellow member function don't need pass reference?
operator+ unbound function, i.e. not fellow member of number so, rule, has no implicit access private members.
the rule not affected passing number objects value or reference. access protection applied every access number object, if private, stack-based copy.
there @ to the lowest degree 3 ways out:
declarenumber operator+(number, number) friend of number add public getter x variable accessible. implement += operator fellow member of class , implement free operator in terms of it: return number(a) += b; c++ object
Comments
Post a Comment