在C++中,默认编译器为每个类创建默认构造函数。但是,如果我们定义自己的构造函数,编译器不会创建默认构造函数。这是因为默认构造函数不接受任何参数,如果创建了两个默认构造函数,编译器很难调用哪个默认构造函数。
null
例如,程序1编译时没有任何错误,但程序2的编译失败,错误为“没有用于调用’myInteger::myInteger()’的匹配函数” 方案1
CPP
#include<iostream> using namespace std; class myInteger { private : int value; //...other things in class }; int main() { myInteger I1; getchar (); return 0; } |
项目2:
CPP
#include<iostream> using namespace std; class myInteger { private : int value; public : myInteger( int v) // parameterized constructor { value = v; } //...other things in class }; int main() { myInteger I1; getchar (); return 0; } |
如果您发现上述GFact中有任何不正确之处,或者您想分享有关上述主题的更多信息,请写评论。 参考资料: http://en.wikipedia.org/wiki/Default_constructor http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr375.htm
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END