0

why is the constructor Number() not being invoked when a number data type is initialized without any values ?

#include <iostream>
using namespace std;

class Number {

   int n;
   public:
      Number() {
          cout<< 0 << " ";    
      }
      Number(int i): n(i) {
          cout<< n << " ";
      }
};

int main()
{
  int i = 1;
  Number n1();
  Number *n2 = new Number(i++);
  Number *n3;
  new Number(i++);
  return 0;
}
  • 1
    `Number n1();` is a function declaration. And there are plenty of dupes for these like this: https://stackoverflow.com/questions/73384406/what-does-static-testclass-testclass-do-in-c/73384518#73384518. – Jason Aug 21 '22 at 06:21
  • `Number n1();` -> `Number n1;` The first declares a function, the second declares a variable. – john Aug 21 '22 at 06:23
  • 1
    sorry to say, but it is the [most vexing parsing](https://en.wikipedia.org/wiki/Most_vexing_parse). You can switch to initializer-list style declaration to avoid such issue - `Number n1{};` – fadedreamz Aug 21 '22 at 06:43

0 Answers0