2

If i have a class like as below.

class A {
int data;
};

A a; // Case1: calling explicit Default constructor
A b(); // Case2: Calling implicit default constructor

What is the difference between Case1 and Case2?

3 Answers3

9
A b();

it does not define an object, it declares a function which returns type A, It's also well known as most vexing parse.

billz
  • 44,644
  • 9
  • 83
  • 100
2
A b(); // Case2: Calling implicit default constructor

The comment is incorrect. A b(); is a function declaration (The function is called b and returns an object of type A and you intend to define the function later) not an object definition.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

Case 1:

    A a; // Case1: calling explicit Default constructor

Calls the implicit default constructor, since you hadn't provided one in the class.

Case 2:

   A b(); // Case2: Calling implicit default constructor

It is the function decleration say "b" is the function name and it takes no argument and returns A object.

Hope it helps.

Saravanan
  • 1,270
  • 1
  • 8
  • 15