why default constructor is not present for a class containing const data members

无人久伴 提交于 2019-12-07 06:43:11

问题


why default constructor is not added by the compiler for the class containing constant data members. please see the below code , in that i have declared constant data member 'a' and while trying to create object for a class 'ClassA' it is saying No Appropriate Default constructor is available . please help.

#include "stdafx.h"
#include <iostream>
using namespace std;

class ClassA
{
    private:
  const int a;
    public :
  void print()
  {
      cout << "hello world" << endl;
  }
};

int main()
{
  ClassA obj;
  obj.print();
  return 0;
}

回答1:


Since a const value cannot change after it is initialized how could a default constructor choose a value for it. So the default constructor is not created




回答2:


The C++03 rule was specified in 12.6.2/4 [class.base.init]. If a non-static member of a class was not mentioned in the member initializer list of a constructor then if it was const qualified it would have to be of a non-POD class type with a user-declared constructor otherwise the program would be ill-formed. A implicitly defined constructor is defined with an empty member initializer list (and empty body) so, in this case, causing the implicitly declared default constructor to be implicitly defined it would also render the program ill-formed.

The C++11 rule amounts to the same thing. Non-static data members which are not specified in the member initializer list are default initialized. In C++11 8.5/6 [dcl.init], "[...] If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor." which boils down to the same rule in this case.




回答3:


Since a is a const variable, you can declare it as static and initialize it, without using a constructor as follows,

class ClassA
{
    private:
    const static int a=10;
    public :
    void print()
    {
      cout << "hello world" << endl;
    }
};

int main()
{
  ClassA obj;
  obj.print();
  return 0;
}



回答4:


The type int does not have a default value in C or C++, therefore the value of a would be undefined. For example, VC++ will populate the value of a with a different default value if it is run in debug to if it is run in release mode.

In debug, VC++ populates uninitialized memory with the following values:

  • 0xCCCCCCCC - Used by Microsoft's C++ debugging runtime library and many DOS environments to mark uninitialized stack memory.
  • 0xCDCDCDCD - Used by Microsoft's C/C++ debug malloc() function to mark uninitialized heap memory, usually returned from HeapAlloc()

So by not initializing a your program will have a different const value each time.




回答5:


I believe you have to explicitly initialize const members in the constructor, you'd have to set them somewhere, and as they are const, you can just be setting it wherever you like!



来源:https://stackoverflow.com/questions/16706674/why-default-constructor-is-not-present-for-a-class-containing-const-data-members

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!