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];
    }
}
Was it helpful?

Solution

myClass can access members of myClass. Access isn't based on the instance, it's based on the class.

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
scroll top