0

I have a C++ program as below

#include <iostram>

using namespace std;

class abc
{
    public:
        abc()
        {
            cout << "in Constructor\n";
        };
};

int main()
{
    abc a();
}

I compiled and I saw that "in Constructor" is not getting printed which means constructor is not getting called. I was wondering that both abc a and abc a() are same and both will create object a. But that seems to be wrong. Can any one please let me know what abc a() means because I didn't get the compilation error also.

kadina
  • 5,042
  • 4
  • 42
  • 83
  • 7
    This is [most vexing parse](https://en.wikipedia.org/wiki/Most_vexing_parse). You're declaring a function, not a variable. – Anton Savin Jun 25 '15 at 18:01
  • 1
    `abc a()` declares a function, not object. – Ajay Jun 25 '15 at 18:01
  • 1
    As Scott Meyers writes, you should do stuff like `abc a{};` to avoid this kind of stuff. – Ami Tavory Jun 25 '15 at 18:04
  • 3
    BTW: You typed 20 letters to avoid typing 5 letters (`std::`) once! Also, you should use `std::end`, which prints a newline **and** flushes the stream, because without flushing the stream it could appear on screen delayed, which isn't something you want while debugging. Or, use `std::cerr`, which is unbuffered. – Ulrich Eckhardt Jun 25 '15 at 18:04
  • 1
    Remove brackets from a() . – mystic_coder Jun 25 '15 at 18:04
  • Create instance like : abc a; – mystic_coder Jun 25 '15 at 18:05
  • @Barry My fault, I knew as soon as I'd clicked the vote button I'd picked the wrong dupe. – Jonathan Potter Jun 25 '15 at 18:07
  • @AntonSavin: This is _not_ the most vexing parse. The article you linked to is worth a read: go ahead and do that now! – Lightness Races in Orbit Jun 25 '15 at 18:16
  • @LightnessRacesinOrbit MVP is not nearly normative term. Also Scott Meyers explicitly mentions this exact case in his Effective STL book. – Anton Savin Jun 25 '15 at 18:29
  • @AntonSavin: He's wrong. There is nothing vexing about a function declaration except that you might have forgotten what function declarations look like when you wrote it. The only logical and reasonable definition for the MVP is according to the examples on that Wikipedia article. – Lightness Races in Orbit Jun 25 '15 at 18:29
  • The wiki article describes how braces can be used to force a variable declaration. But how do we force a function declaration in such a way as to suppress clang's warnings? i.e. I want warnings when I'm being "ambiguous", but I want to be able to disambiguate for either option. (*Update* - I guess using an intermediate `typedef` would work - anything simpler?) – Aaron McDaid Jun 26 '15 at 09:58

0 Answers0