C++ passing an object to a function, the operator= is not called
So here is the code snippet:
class MyClass { public: MyClass(char chIn) { std::cout << "Constructor!" << std::endl; }
MyClass & operator= (char chIn) { std::cout << "Assigment operator!" << std::endl; } } ;
void Func(MyClass objIn) { return; }
int _tmain(int argc, _TCHAR* argv[]) { Func('T'); system("PAUSE"); return 0; }
In the upper example the constructor of the object is called!!!! Why is this behavior? Shouldn't the assigment operator be called? Because we're assigning a value to the function parameter, aren't we?
operator =调用已存在的对象,否则使用构造函数(或复制构造函数)创建所需的实例
链接地址: http://www.djcxy.com/p/73088.html上一篇: 提取运算符重载中的c ++未定义引用