您的位置:首页 > 其它

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: