类使用重载运算符返回变量值

#include <iostream>
using namespace std;

class Base
{
public:
	int Numb;
        int Year;
public:
	Base():Numb(1) {};
	~Base() {};
	Base& operator = (size_t s)
	{
		Base bb;
		bb.Numb = s;
		return bb;
	};
	operator int() const
	{
		return Numb;
	};
        int operator() () const
	{
		return Year;
	};
};

class Son
{
public:
	Son() {};
	~Son() {};
	Base base;
	void Test()
	{
		base.Numb = 9;
	}
};

void main()
{
	Base bas;
	bas.Numb = 3;
	cout << bas.Numb << endl;
	cout << "bas:" << bas << endl;
        cout << "bas:" << bas() << endl;
}

运行结果:

3
bas:3
bas:5

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页