2

I am a newer in C++. I am just learning the C++ default constructor. I am confused with the following code.

#include <iostream>
class A
{
    public:
        A()
        {
            std::cout << "default constructor(no parameter)." << std::endl;
        }
        A(int a = 1, int b = 3)
        {
            std::cout << "default constructor(parameters have default value)." << std::endl;
            std::cout << "a:" << a << " b:" << b << std::endl;
        }
        ~A()
        {
            std::cout << "~A" << std::endl;
        }
};

int main(void)
{
    //A a;   // complie error: call of overloaded 'A()' is ambiguous.

    A a1();  // OK. but what does it do?

    A a2(2); // OK. call the A(int, int)
    return 0;
}

my GCC is 4.9.2.

  • one: A() and A(int a = 1, int b = 2) are the default constructor?
  • two: When I call A a1(); It outputted nothing.Why?
  • three: according to the output, A a2(2) call the A(int a=1, int b=2), is that correct?

my result of this code is

default constructor(parameters have default value).
a:2 b:3
~A

A a1() outputted nothing???

JCRunner
  • 125
  • 1
  • 1
  • 7

0 Answers0