0

My question is on Default Constructors in C++. After passing the above two objects separately, I found that (a) A obj1, was detected as Default Constructor, and the constructor defining it was executed. (b) A obj1(), was not detected as Default Constructor. It matched with none of the constructors.

In both the cases, there is no argument passed. Then why is that only (a) is set for Default Constructor and not the second, i.e (b).

  • Your question is about C++, so where did the [c] tag come from? – Ben Voigt Apr 29 '18 at 18:08
  • 3
    I strongly suspect you've run afoul of declarations preferring the interpretation as a function (ala "Most Vexing Parse"), but since you haven't shown any actual code I can't tell for sure. – Ben Voigt Apr 29 '18 at 18:09
  • 5
    `A obj1();` is a function declaration, not object construction. – Jesper Juhl Apr 29 '18 at 18:10
  • C programmers interested in this question may also answer... – Omkar Deshpande Apr 29 '18 at 18:10
  • 3
    @OmkarDeshpande: C has no constructors and no default arguments. Only programmers who know C++ will be able to answer. – Ben Voigt Apr 29 '18 at 18:11
  • 1
    @OmkarDeshpande because of things like this [tag:c] programmers are not [tag:c++] programmers. They might do c++ programs. But I for example, don't care about answering c++ questions and came here just because of the [tag:c] tag. – Iharob Al Asimi Apr 29 '18 at 18:12
  • If I've correctly interpreted your very unclear question, it is a duplicate of [C++ FAQ: Default constructor with empty brackets](https://stackoverflow.com/q/180172/103167) – Ben Voigt Apr 29 '18 at 18:14
  • I am sorry to have consumed your time unnecessarily... @BenVoigt and other C Programmers. Will ensure to provide proper tags next time... – Omkar Deshpande Apr 29 '18 at 18:16
  • 1
    Possible duplicate of [Why is there no call to the constructor?](https://stackoverflow.com/questions/3810570/why-is-there-no-call-to-the-constructor) – Retired Ninja Apr 29 '18 at 18:16
  • 1
    @RetiredNinja: Yay another one for the dupe web. – Ben Voigt Apr 29 '18 at 18:17
  • @BenVoigt I never know if it's THE most vexing parse, A lesser vexing parse, or my best friend's sister's boyfriend's brother's girlfriend heard from this guy who knows this kid who's going with the girl who saw a vexing parse at 31 Flavors last night. I guess it's pretty serious. – Retired Ninja Apr 29 '18 at 18:25
  • 1
    @RetiredNinja: Like you, I'm pretty sure this is near kin to the Most Vexing Parse, but not the MVP himself. It's my impression that "ala" means "in the style of" -- but I don't speak French myself so I might have used it wrong. BTW I like the title of the dupe you found, perhaps we should change the title on the FAQ entry. – Ben Voigt Apr 29 '18 at 18:29

1 Answers1

1

Leta there be a class named A. What is the difference between passing the following two objects: (a) A obj1 and (b) A obj1()?

The difference is that A obj1; declares an object obj1 of type A. While A obj1(); declares a function obj1 which takes no arguments and returns an A - it does not create an object.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70