I want my "no parameters" constructor to call another constructor, with some default constant value (as, if no value was delievered, use the default value);
I used an initialize list for the "no parameters" constructor, which calls the another constructor, with DEFAULT
as a parameter. There, I I used an initialize list to initialize the constant value.
class Foo{
public:
Foo() : Foo(DEFAULT) {};
Foo(int num) : DEFAULT(4) { /*do something with num */};
const int DEFAULT;
};
My problem here is that DEFAULT
is initialized only after I called the other constructor - so yes, DEFAULT
will be 4
, but num
will have some garbage value.
I tried to change the order, but it doesn't seem to work, as I'm getting the same result.
class Foo{
public:
const int DEFAULT;
Foo(int num) : DEFAULT(4) { /*do something with num */};
Foo() : Foo(DEFAULT) {};
};
I would be glad to know how can I overcome this problem - how to use a constant value of a class inside the class constructor?