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];
    }
}
有帮助吗?

解决方案

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

其他提示

Access rights are class based, the object you use in member-functions does not restrict your access.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top