1

I have a class A with a default constructor and a class B with a constructor that takes an object of class A as sole argument.

As a natural way to Create an object of class B I would consider

B b(A());

But the compiler interprets this as a function declaration.

I know that

B b((A()));

or

B b {A()};

are alternatives. But my question is: How can the first version be interpreted as a function declaration? From my point of view it doesn't look like one.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45

1 Answers1

3

How can the first version be interpreted as a function declaration?

It's just a language rule: In C++ everything that can be interpreted as a declaration will be interpreted as a such.


The following code:

B b(A());

can be interpreted as a declaration of a function whose name is b which:

  • Returns an object of type B.

  • Takes a (pointer to a) function as a parameter (the name of the parameter is not provided) that returns an object of type A.

So it will interpreted as a declaration be cause it can be interpreted as a such.

JFMR
  • 23,265
  • 4
  • 52
  • 76