首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求解,关于构造函数满载

2012-09-25 
求解,关于构造函数重载[codeC/C++][/code]#include stdafx.h#include iostream.hclass Rectangle{pri

求解,关于构造函数重载
[code=C/C++][/code]#include "stdafx.h"
#include "iostream.h"

class Rectangle
{
private:
int itlength,itwidth;
public:
Rectangle(int x);
Rectangle(int l=0,int w=0);

};

Rectangle::Rectangle(int x)
{
itlength=x;
itwidth=2;

cout<<"ni hao !"<<endl;
}


Rectangle::Rectangle(int l,int w)
{
itlength=l;
itwidth=w;
cout<<itlength<<"+"<<itwidth<<endl;

}




int main(int argc, char* argv[])
{
 Rectangle a(1);

Rectangle b(1,2);

return 0;
}







[解决办法]
Rectangle(int x);
Rectangle(int l=0,int w=0);

这样会引起二义性吧
你调用的时候用1个参数,到底找哪个?
第一个没必要,直接第二个就可以了

下面的这个也是会引发二义性
Rectangle(){itlength=0;itwidth=0;};
Rectangle(int l=0,int w=0);

只能用1个

热点排行