myClass can access members of myClass. Access isn't based on the instance, it's based on the class.
Why are no get functions needed in assignment operator overloads?
-
03-07-2023 - |
Question
I am just curious as to why this is. Is it simply the magical quality of the overload's definition being inside the class that both objects are made from? I was thinking about it and it felt just slightly strange to me that one function has access to member data of two objects.
myClass& myClass::operator= (const myClass& a){
// shallow copy
arraySize = a.arraySize; // no get functions needed
// deep copy
theArray = new int[arraySize]; // no get functions needed
for (int i = 0; i < arraySize; i++){
theArray[i] = a.theArray[i];
}
}
Solution
OTHER TIPS
Access rights are class based, the object you use in member-functions does not restrict your access.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow