Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
}
like image 366
nagaradderKantesh Avatar asked May 23 '13 06:05

nagaradderKantesh


People also ask

Can a class have no default constructor?

No default constructor is created for a class that has any constant or reference type members.

Is it always necessary to provide a default constructor for a class Why or why not?

Answer: C++ Empty constructor necessity depends upon class design requirements. We know that C++ class constructor is called when we create an object of a class. If a class is not required to initialize its data member or does not contain data member, there is no need to write empty constructor explicitly.

What happens if default constructor is not defined?

ANSWER: a. If default constructor is not defined, then how the objects of the class will be created? b. Error will occur at run-time.

Can we declare constructor as const?

Constructors may be declared as inline , explicit , friend , or constexpr . A constructor can initialize an object that has been declared as const , volatile or const volatile . The object becomes const after the constructor completes.


4 Answers

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.

like image 124
CB Bailey Avatar answered Oct 05 '22 17:10

CB Bailey


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

like image 36
aaronman Avatar answered Oct 05 '22 16:10

aaronman


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;
}
like image 21
Deepu Avatar answered Oct 05 '22 17:10

Deepu


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.

like image 36
Steve Avatar answered Oct 05 '22 16:10

Steve