4

Possible Duplicate:
uninitialized const

I understand that a const object needs to initialized.

So for the following code,

class sample
{};

int main()
{
   const sample obj;
   return 0;
}

the compiler will complain because the const object obj is not initialized.

But when i modify the code(show below) with a default constructor, the compiler will not throw any error.

class sample
{
    public:
       sample() { }
};

int main()
{
    const sample obj;
    return 0;
}

What is the thing that the newly added default ctor does which satisfies the compiler?

Community
  • 1
  • 1
nitin_cherian
  • 6,405
  • 21
  • 76
  • 127

2 Answers2

5

What is the thing that the newly added default ctor does which satisfies the compiler?

Because that is the requirement imposed by the C++ standard when declaring objects with the const qualifer.

Reference:

C++03 8.5 Initializers 8 Declarators
§9:

If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (or array thereof), the object shall be default-initialized; if the object is of const-qualified type, the underlying class type shall have a user-declared default constructor. Otherwise, if no initializer is specified for a nonstatic object, the object and its subobjects, if any, have an indeterminate initial value90); if the object or any of its subobjects are of const-qualified type, the program is ill-formed.

user4581301
  • 33,082
  • 7
  • 33
  • 54
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • +1. This is more complete answer. – Nawaz Jan 21 '12 at 10:48
  • @Als: I could not understand from the standard. Could you explain the standard in your words. I do not understand the phrase `default-initialized` also. – nitin_cherian Jan 21 '12 at 10:51
  • 1
    @LinuxPenseur: C++ standard classify's the declaration of an object in to 3 possible ways: **Default Initialization**,**Value Initialization** & **Zero Initialization**.The way you create your object amounts to Default Initialization.Perhaps [this](http://stackoverflow.com/questions/6032638/default-variable-value/6032889#6032889) answer of mine might be of help to understand what each means.If not, do ask. – Alok Save Jan 21 '12 at 10:54
  • @Als: Thanks for the link. I am on track. Perhaps your answer to this question may help. Why can't the compiler generated ctor `default initialize` the const object in the first sample code? – nitin_cherian Jan 21 '12 at 11:07
  • 2
    @LinuxPenseur - The compiler generated constructor does not initialize types that the C language does not initialize. If it did, C++ would lose tons of benchmarks to C, all over the internet. – Bo Persson Jan 21 '12 at 11:25
2

You're doing a default initialization of a const-qualified type. The C++ (C++11 draft n3290) standard has this to say about that (§8.5/6 Initializers):

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.

Your first sample doesn't conform to this (no user-provided constructor). The second does.

Mat
  • 202,337
  • 40
  • 393
  • 406