Does compiler create default constructor when we write our own?
2013-11-26 10:11
495 查看
In C++, compiler by default creates default constructor for every class. But, if we define our own constructor, compiler doesn’t create the default constructor.
For example, program 1 compiles without any error, but compilation of program 2 fails with error “no matching function for call to `myInteger::myInteger()’ ”
Program 1
#include<iostream> using namespace std; class myInteger { private: int value; //...other things in class }; int main() { myInteger I1; getchar(); return 0; }
Program 2
#include<iostream> using namespace std; class myInteger { private: int value; public: myInteger(int v) // parametrized constructor { value = v; } //...other things in class }; int main() { myInteger I1; getchar(); return 0; }
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
转载请注明:http://www.cnblogs.com/iloveyouforever/
2013-11-26 10:11:05
相关文章推荐
- When should we write our own copy constructor?
- When does compiler create default and copy constructors in C++?
- When should we write our own assignment operator in C++?
- When Do We Write Our Chinese OS ? (1)
- When Do We Write Our Chinese Os? (2)
- When Do We Write Our Chinese OS? (3)
- When should I use RequestProcessor.getDefault() and when should I create my own RequestProcessor?
- how to solve "java.sql.Timestamp does not have a no-arg default constructor" ?
- compiler-generated default constructor
- Why does Python print unicode characters when the default encoding is ASCII?
- checking for C++ compiler default output... configure: error: C++ compiler cannot create executable
- When we call objective c is runtime language what does it mean? 我们说的oc是动态运行时语言是什么意思?
- Java SE series:1. environment configure and Hello world! [We use compiler and packager to create an application!]
- checking for C compiler default output file name... configure: error: C compiler cannot create executables
- Coding Style to implement our own function new() -- when using vmm_data_member_begin
- Resolving: java.sql.Timestamp does not have a no-arg default constructor
- when we are young,we are try our best to learn
- [ASP.NET AJAX]Important notes when we create web service methods for some extenders in AjaxControlToolkit package
- Process when we start our testing work
- how to solve "java.sql.Timestamp does not have a no-arg default constructor" ? .