2

In class A,counter b(5); doesn't work.

while counter a; (the call of the default constructor) works.

Why? Is it not possible to call a parameterized constructor for an instance variable?

class counter {
private:
  int value;
public:
  counter() { this->value = 0; }
  counter(int value) { this->value = value; }
};

class A {
  private:
    // works
    counter a;
    // works (since C++11)
    int x = 5; 
    // doesn't work
    counter b(5);
};

int main()
{
// works
counter myCounter;
// works as well
counter mySecondCounter(5);
return 0;
}
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
knowledge
  • 941
  • 1
  • 11
  • 26
  • 1
    You don't need `this->`. And your argument and member have the same name. Rename it to something like `v` or `value_`. – dani May 04 '17 at 07:18
  • Also, use an inizialisation list: `counter() : value(0) {}` and `counter(int v) : value(v) {}`. Or even use a default argument, then you have only one constructor. This is more expressive: your counter normally starts at 0, except if you explicitly want to start it from somewhere else. Further, make it capital: `Counter`. – dani May 04 '17 at 07:20
  • 1
    Inside a class definition, `some_type some_name(` makes the compiler expect a member function declaration. – molbdnilo May 04 '17 at 07:39
  • 2
    Possible duplicate of [Why C++11 in-class initializer cannot use parentheses?](http://stackoverflow.com/questions/24836526/why-c11-in-class-initializer-cannot-use-parentheses) – Saurav Sahu May 04 '17 at 07:46
  • @SauravSahu same answer, different question – pqnet Feb 04 '19 at 14:44

4 Answers4

3

If allowed, counter b(5); technically declares a function named b that returns a counter object by value, and takes a single argument that's an integer.

While that, very likely, would not be the intention of the coder, that's why it is not allowed.

In Bjarne's words about this rule for In-Class Initializers:

We can specify an initializer for a non-static data member in the class declaration. For example:

class A {
  public:
    int a {7};
    int b = 77;
};

For pretty obscure technical reasons related to parsing and name lookup, the {} and = initializer notations can be used for in-class member initializers, but the () notation cannot.

Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
1

It is possible. Change

counter b(5);

to

counter b = counter(5);

It is perhaps more elegant to initialise in a constructor intialisation list.

class A {
private:
    A() : b(5) {}

    counter a;
    int x = 5;
    counter b;
};
lfgtm
  • 1,037
  • 6
  • 19
1

You have four options:

class A {
private:
    counter C1{5};

    // Works, only with single parameter constructors
    counter C2 = 5;

    // Works, with any number of parameters
    counter C3 = counter(5);

    counter C4;

    // Constructor initializer list, works with any number of parameters
    A() : C4(5) {}
};
Jonas
  • 6,915
  • 8
  • 35
  • 53
1

In C++, this is not the correct syntax for initializing members with default values.

There are two options to solve it:

1.use operator =

counter b = counter(5);

2.use constructor with initialization list

A() : a(),b(5) {}
ibezito
  • 5,782
  • 2
  • 22
  • 46