代码编译C++函数重载

最近使用开发的过程中出现了一个小问题,顺便记录一下原因和方法--代码编译

#include "stdafx.h"


#include <iostream>
using namespace std;

class A
{
public:
	A(int a = 10)
	{
		b = a;
		cout << b << endl;
	}
public:
	int b;
};	


int _tmain(int argc, _TCHAR* argv[])
{

	A a;
	A b(3);	

	system("pause");
	return 0;
}

    以上代码可以常正运行,以下代码编译错出 。

// CPPTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include <iostream>
using namespace std;

class A
{
public:
	A(int a = 10)
	{
		b = a;
		cout << b << endl;
	}
	A()
	{
		b = 0;
		cout << b << endl;
	}
public:
	int b;
};	


int _tmain(int argc, _TCHAR* argv[])
{

	A a;
	A b(3);	

	system("pause");
	return 0;
}

    以下代码可以常正编译,执行,但是结果和假想有点差距。A a()不是义定变量a,而是申明白一个数函原型,该数函返回A,并且不接受任何参数

    每日一道理
当浮华给予我们过多欺骗,现实中的虚假几乎让我们忘却了真的存在,是真情唤回了迷离的心,是真情带给了我们最纯、最真的感觉,它流露的是美的誓言,渗透的是永恒执著的真爱。
// CPPTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include <iostream>
using namespace std;

class A
{
public:
	A(int a = 10)
	{
		b = a;
		cout << b << endl;
	}
	A()
	{
		b = 0;
		cout << b << endl;
	}
public:
	int b;
};	


int _tmain(int argc, _TCHAR* argv[])
{

	A a();
	A b(3);	

	system("pause");
	return 0;
}

    确正的改修方法如下

// CPPTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include <iostream>
using namespace std;

class A
{
public:
	A(int a = 10)
	{
		b = a;
		cout << b << endl;
	}
	//A()
	//{
	//	b = 0;
	//	cout << b << endl;
	//}
public:
	int b;
};	

A PrintA()
{
	return A(5);
}


A a()
{
	cout << "in A a() function" << endl;
	return A(6);
}

int _tmain(int argc, _TCHAR* argv[])
{

	A a();
	A b(3);

	a();

	system("pause");
	return 0;
}

    

    

    

文章结束给大家分享下程序员的一些笑话语录: 与女友分手两月有余,精神萎靡,面带菜色。家人介绍一女孩,昨日与其相亲。女孩果然漂亮,一向吝啬的我决定破例请她吃晚饭。
选了一个蛮贵的西餐厅,点了比较贵的菜。女孩眉开眼笑,与我谈得很投机。聊着聊着,她说:“我给你讲个笑话吧。”“ok”
  “一只螳螂要给一只雌蝴蝶介绍对象,见面时发现对方是只雄蜘蛛。见面后螳螂问蝴蝶‘如何?’,‘他长的太难看了’,‘别看人家长的丑,人家还有网站呢’。”
  “呵呵………”我笑。忽然她问:“你有网站吗?”  

posted @ 2013-04-28 19:52  坚固66  阅读(242)  评论(0)    收藏  举报