假设:如果将=运算符不定义为成员函数,那么此时的=是否还是赋值运算符。
结论:operator=必须是成员函数
Test.h
#pragma once
class Test
{
public:
Test() = default;
private:
int data;
};
Test.cpp
#include<iostream>
#include"Test.h"
Test operator=(Test a, Test b) // error :operator=必须是成员函数
{
}
int main()
{
return 0;
}
但是operator<可以不是成员函数
#include
#include"Test.h"
Test operator<(Test a, Test b) //正确,注意operator<能且只能接受两个参数
{
;
}
int main()
{
return 0;
}