ask for constructor
问一个初级的问题:如下
class test
{
test()
{
cout < < "Construct by a integer " < < endl;
}
};
test()叫default constructor.
那么以下:
class test
{
test(int nTest = 0)
{
cout < < "Construct by a integer " < < endl;
}
};
带参数的test函数还叫default constructor吗?还是另有别名?
除了default constructor、copy constructor,还有别的constructor吗?assignment是operator,不是constructor??
请说一说
[解决办法]
还有默认构造函数
test(test&) copy constructor
[解决办法]
//You can write all of these constructor in a class:
//////////////////////////////////////////////////
class Test
{
public:
Test()
{
cout < < "defaut constructor\n ";
}
Test(int i)
{
cout < < "overlap constructor\n ";
}
Tet(const Test&t)
{
cout < < "copy constructor\n ";
}
//More................................
};
int main()
{
Test t1;//It will call Test()
Test t2(77);//It will call Test(int i)
Test t3(t2);//It will call Test(const Test&t)
//more...................................
return 0;
}
[解决办法]
defaut constructor有两种情况:
提供了不带参数的constructor
或者是程序员没提供任何constructor
constructor(const ClassName & objectName)参数是自身类的引用时,叫copy constructor
其他overload的constructor只要符合C++中函数重载条件的,也是constructor
[解决办法]
我想是overlap 笔误吧
overload