# 重载构造函数的调用？

0

```#include <iostream>
using std::cout;
using std::endl;

class Counter
{
public:
Counter();
Counter(int val);
~Counter(){}
int GetItsVal() const { return itsVal; }
void SetItsVal(int x) { itsVal = x;}
private:
int itsVal;
};

Counter::Counter():itsVal(0)
{}

Counter::Counter(int val):itsVal(val)          //是怎样调用这个函数的？不明白？？？？？
{}

int main()
{
int theShort = 5;
Counter theCtr = theShort;                 //经过测试，等价于 Counter theCtr = Counter(theShort);和Counter theCtr(theShort); 请解释一下，谢谢……
cout << "theCtr: " << theCtr.GetItsVal() << endl;
system("pause");
return 0;
}```
waynewuzhenbo | 初学一级 | 园豆：189

0

Counter counter;

Counter counter();

Counter counter = Counter();

Counter counter = 5

Counter counter(5)

Counter counter = new Counter(5)

Counter counter = (5)

Counter counter = 5

Counter::Counter(int val):itsVal(val)

{

}

Counter::Counter(int val)

{

itsVal = val;

}

```#include <iostream>
using std::cout;
using std::endl;

class Counter
{
public:
Counter();
Counter(int val);
Counter(int val, int val2);
~Counter(){}
int GetItsVal() const { return itsVal; }
void SetItsVal(int x) { itsVal = x;}
private:
int itsVal;
int itsVal2;
};

Counter::Counter():itsVal(0)
{}

Counter::Counter(int val):itsVal(val)          //是怎样调用这个函数的？不明白？？？？？
{}

Counter::Counter(int val, int val2):itsVal(val), itsVal(val2)          //是怎样调用这个函数的？不明白？？？？？
{}

int main()
{
int theShort = 5;
Counter theCtr = theShort;                 //经过测试，等价于 Counter theCtr = Counter(theShort);和Counter theCtr(theShort); 请解释一下，谢谢……
cout << "theCtr: " << theCtr.GetItsVal() << endl;
system("pause");
return 0;
}```

Counter::Counter(int val, int val2):itsVal(val), itsVal(val2)

@笨笨蜗牛:

Counter::Counter(int val):itsVal(val)

{}

Counter::Counter(int val)

{

::itsVal = val;

}

Counter::Counter(int val)

{

int myVal = itsVal;

itsVal = val;

}

Counter::Counter(int val):itsVal(val)

{

int myVal = itsVal;

}

@笨笨蜗牛:

waynewuzhenbo | 园豆：189 (初学一级) | 2012-05-19 16:45

waynewuzhenbo | 园豆：189 (初学一级) | 2012-05-26 12:28

您需要登录以后才能回答，未注册用户请先注册