Sumeet
Sumeet

Reputation: 8292

Why no other constructor is being allowed?

I am learning about initializer list and learnt that const members must be initialized using it, because you cannot initialize it using default constructor or parameterised constructor.

class Foo
{
private:
    const int y;            
public:         
    Foo(int yy) :y(yy){}        
    int getY();
};

Now suppose if I have another member int x; not a const,Why can't I initialize it using default constructor, What is the idea behind this restriction?

The code that gives error:

class Foo
{
private:
    const int y;    
    int x;
public:     
    Foo(int yy) :y(yy){}
    Foo()
    {
        x = 100;
    }
    int getY();
};

Upvotes: 1

Views: 102

Answers (3)

eerorika
eerorika

Reputation: 238301

I am learning about initializer list and learnt that const members must be initialized using it, because you cannot initialize it using default constructor or parameterised constructor.

Const members can be initialized in the member initializer list of both a default constructor and any parametrised constructor. (A default constructor is a constructor that can be invoked without parameters.)

Now suppose if I have another member int x; not a const,Why can't I initialize it using default constructor, What is the idea behind this restriction?

You can initialize any number of members (there is probably some implementation defined limit, but it's not relevant to this question) in the default constructor. There is no such restriction that you describe.

Demo, class with two members, both initialized in the default constructor:

struct Foo {
    const int y;
    int       x;
    Foo(): y(1), x(100){}
};

Edit for the mcve.

The code that gives error:

class Foo
{
private:
    const int y;    
    int x;
public:     
    Foo(int yy) :y(yy){}
    Foo()
    {
        x = 100;
    }
    int getY();
};

All constructors must initialize const members. Your parametrised constructor does initialize y, but the default constructor doesn't. That is why it doesn't work. See my demo above for a working example.

PS. Your parametrised constructor doesn't initialize x, but that is OK: x isn't const, so you can assign a value to it later.


In my code if I have a parameterised constructor like Foo(int xx) { x = xx;}

It will not give any error

This program:

struct Foo {
    const int y;
    int       x;
    Foo(int xx) { x = xx;}
};

Is ill formed in standard C++. If your compiler accepts it without a warning, then it isn't standard compliant.

Upvotes: 4

Shlublu
Shlublu

Reputation: 11027

I am not sure I understood the question properly. To summarize what you can and can't do:

// .h
class MyClass
{
private:
    int x;
    const int y;

public:
    MyClass();
    MyClass(int initValue);
};

// .cpp
// The following is legal
MyClass::MyClass() 
: x(0), y(0) 
{}     

// Alternatively, the following is legal too. 
MyClass::MyClass() 
: y(0) 
{ 
    x = 0; 
}   

// This is not legal: y cannot be initialized that way as it is const.
// No problem for x though.
MyClass::MyClass() 
{ 
    x = 0; 
    y = 0; // You cannot do that
} 

// The following is legal
MyClass::MyClass(int initValue) 
: x(initValue), y(initValue) 
{}     

// Alternatively, the following is legal too. 
MyClass::MyClass(int initValue) 
: y(initValue) 
{ 
    x = initValue; 
}   

// This is not legal: y cannot be initialized that way as it is const.
// No problem for x though.
MyClass::MyClass(int initValue) 
{ 
    x = initValue; 
    y = initValue; // You cannot do that
} 

Of course, you can only have one CTor per declared signature. The examples above are 3 alternatives - 2 legal and 1 illegal - per declared signature.

Upvotes: 0

RuudSieb
RuudSieb

Reputation: 623

Unclear what you're trying to accomplish. Following code simply compiles

class Foo
{
private:
    int _nonConst;
    const int _const;

public:
    Foo() : _const(10), _nonConst(11)
    {

    }

    Foo(int x) : _const(x), _nonConst(x)
    {

    }
};

Upvotes: 1

Related Questions