0

Why does this code

class A
{
public:
    A()
    {
        cout<<"Class A!";
    }
    A(int a=0)
    {
        cout<<"A";
    }
};

int main()
{
    A a();
    return 0;
}    

... result in no output?

But if instead of A a() I place any value, then the output is "A". And I can't just simply type A a because it says it's ambiguous.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Why isn't it a duplicate exactly? You're asking why you get no output. The output is in your class constructor, which is not getting called. The reason why is in the duplicate - you're not creating an object of that class. – Mat Apr 03 '18 at 07:53
  • Write `A a;` instead of `A a();` BTW: Your code is broken, because both of your constructors can be called without args. That is not possible! – Klaus Apr 03 '18 at 07:55
  • I know, it was just a tricky thing I saw in a quiz and didn't know why there's no output. – whyLikeThis Apr 03 '18 at 07:57
  • You get no output because you did not create an object. You simply define a function a which returns an object of type A. All that is written in the linked duplicate! – Klaus Apr 03 '18 at 07:59

0 Answers0