类的const和非const成员函数的重载

1. 类的const成员函数与非const成员函数的重载

#include <iostream>
#include <string>
using namespace std;

class Person
{
public:
	Person() {}

	Person(string name, string address) : mName(name), mAddress(address){}

	string& GetPersonName()
	{
		return mName;
	}

	string& GetPersonAddress()
	{
		return mAddress;
	}

private:
	string mName;
	string mAddress;
};

int main()
{
	Person a("zhangguanghui", "hebei Province");
	cout << a.GetPersonName() << " " << a.GetPersonAddress() << endl;

	const Person b("zhangjingru", "shanxi Province");
	cout << b.GetPersonName() << " " << b.GetPersonAddress() << endl;

	return 0;
}

输出结果是: 

该文件不能通过编译,给出的错误时:

1>d:\visual c++\c++primer\第12章\12_1\12_1\person.cpp(33) : error C2662: “Person::GetPersonName”: 不能将“this”指针从“const Person”转换为“Person &”

1>d:\visual c++\c++primer\第12章\12_1\12_1\person.cpp(33) : error C2662: “Person::GetPersonAddress”: 不能将“this”指针从“const Person”转换为“Person &”

我们知道每个类的成员函数都会隐含得插入一个this指针,则一个非const成员函数插入的隐含this指针是Person &, 

每一个const成员函数插入的隐含this指针是const Person &.

因此GetPersonName是一个非const成员函数,故插入的this指针是Person &,然而const Person b对象是一个const对象,所以在使用b调用GetPersName时

需要将const Person 转换成Person &,我们知道将const对象转换成非const对象是不合法的。所以编译没有通过。 同理GetPersonAddress函数也一样。

我们还知道可以将一个非const对象转换成一个const对象,所以一个非const对象可以调用const成员函数。

修改正确代码为:

#include <iostream>
#include <string>
using namespace std;

class Person
{
public:
	Person() {}

	Person(string name, string address) : mName(name), mAddress(address){}

	const string& GetPersonName() const
	{
		return mName;
	}

	const string& GetPersonAddress() const
	{
		return mAddress;
	}

	string& GetPersonName()
	{
		return mName;
	}

	string& GetPersonAddress()
	{
		return mAddress;
	}

private:
	string mName;
	string mAddress;
};

int main()
{
	Person a("zhangguanghui", "hebei Province");
	cout << a.GetPersonName() << " " << a.GetPersonAddress() << endl;

	const Person b("xiaozhang", "shanxi Province");
	cout << b.GetPersonName() << " " << b.GetPersonAddress() << endl;

	return 0;
}

正确执行结果是:



这里有一个思考:为什么我们要定义 const string& GetPersonName() const;  const string& GetPersonAddress() const.

而不是string& GetPersonName() const; string& GetPersonAddress() const。

这是一篇文章,可以读一读:http://blog.csdn.net/anjy/article/details/1819126


从上面例子我们得出: const对象只能调用const成员函数,非const对象可以调用非const成员函数,也可以调用const成员函数,其中非const成员函数优先。

2.  下面是一个微软2013年实习生招聘笔试的一个题目:

#include <iostream>
using namespace std;

class A  
{  
public:  
	virtual void f()  
	{  
		cout<<"A::f"<<endl;  
	}  
	void f() const  
	{  
		cout<<"A::f const"<<endl;  
	}
};  

class B:public A  
{  
public:  
	virtual void f()  
	{  
		cout<<"B::f"<<endl;  
	}  
	void f() const  
	{  
		cout<<"B::f const"<<endl;  
	}  
};  

void g(const A* a)  
{  
	a->f();  
}  

int main()  
{  
	A *b = new B();  
	b->f();  
	g(b);  
	return 0;  
}  




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值