Domanda

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];
    }
}
È stato utile?

Soluzione

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

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top