[英]Why are no get functions needed in assignment operator overloads?
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. myClass可以访问myClass的成员。 Access isn't based on the instance, it's based on the class.
访问不是基于实例,而是基于类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.