码迷,mamicode.com
首页 > 其他好文 > 详细

两种函数不能重载的情况

时间:2015-07-28 16:06:17      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:c++

首先介绍构成函数重载的条件:函数的参数类型不同,参数个数不同才能构成函数的重载


情况一:  参数完全相同,只有返回值不同

示例 :

void print();

int print();

由于只有返回值不同故无法区分到底调用那个函数,因此不能重载


情况二: 语意不明确

示例:

KK(int x , int y = 10){
this->x = 10;
this->y = y;
}
KK(int x){
this->x = 10;
this->y = 10;
}


完整程序

#include <cstdio>
#include <iostream>

using namespace std;

typedef class KK{
public:
	KK(int x , int y = 10){
		this->x = 10;
		this->y = y;
	}
	KK(int x){
		this->x = 10;
		this->y = 10;
	}
	~KK(){

	}
	int x;
	int y;
	void print(){
		cout << x << endl << y << endl;
	}
}*LPPoint, Point;

int main(){
	LPPoint pt = (LPPoint)(new Point(3));

	pt->x = 5;
	pt->y = 5;

	pt->print();

	cout << sizeof(Point) << " " << sizeof(int) << endl;
	delete pt;

	return 0;
}


VS2013报错信息



技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

两种函数不能重载的情况

标签:c++

原文地址:http://blog.csdn.net/u010003835/article/details/47105341

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!