Frage

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];
    }
}
War es hilfreich?

Lösung

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

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top