c++需要在类内重载的运算符

在类中重载的运算符有:(),->,[],=,4种,这里我用一种,其他都差不多了

class CStu
{
	int a;
	int b;
	int c;
	int d;
public:
	CStu()
	{
		a = 12;
		b = 13;
		c = 14;
		d = 15;
	}

int & operator[](int c)
	{
		switch (c)
		{
		case 0:
			return a;
		case 1:
			return b;
		case 2:
			return c;
		case 3:
			return d;
		default:
			break;
		}
	}

};

int main()
{
	CStu st;
	st[1] = 20;
	cout << st[1] << endl;
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/piyixia/article/details/89056186