C/C++_兩種函式不能過載的情況

高達一號發表於2015-07-28

首先介紹構成函式過載的條件:函式的引數型別不同,引數個數不同才能構成函式的過載


情況一:  引數完全相同,只有返回值不同

示例 :

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報錯資訊





相關文章