简体   繁体   English

为什么赋值运算符重载中不需要get函数?

[英]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.

相关问题 操作员超载以进行参考分配 - Operator overloads for reference assignment 赋值运算符的布尔和字符串重载(C ++) - Boolean and String Overloads of the Assignment Operator (C++) C++ - 关于友元赋值运算符重载 - C++ - On friend assignment-operator overloads 使用下标和等于运算符重载进行赋值和检索 - assignment and retreval using subscript and equals operator overloads 为什么在复制赋值运算符的定义中需要删除? - Why delete is needed in the definition of the copy-assignment operator? 当需要移动赋值运算符时,为什么编译器正在寻找复制赋值运算符? - Why compiler is looking for copy assignment operator when move assignment operator is needed? 为什么需要&运算符来处理成员函数的地址,而不需要全局函数的地址? - Why & operator is needed for taking address of member functions but not for global functions? 如何为自定义字符串类型编写构造函数/赋值运算符重载? - How to write constructor/assignment operator overloads for custom string types? 处理赋值运算符重载; 您可以重新分配参考吗? - dealing with assignment operator overloads; can you re-assign a reference? 赋值运算符重载具有类似的转换(仅在VS中) - Assignment operator overloads have similar conversions (only in VS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM