读书人

求解关于构造函数满载

发布时间: 2012-09-25 09:55:59 作者: rapoo

求解,关于构造函数重载
[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个

读书人网 >C++

热点推荐