C++ 有参构造函数的三种调用方法

class Test

{

private:

    int a;
    int b;

public:

    //带参数的构造函数

    Test(int a)
    {

        print("a:%d", a);

    }

    Test(int a, int b)
    {

        print("a:%d b:%d", a, b);

    }

};

int main()

{

    //1. 括号法:C++编译器调用有参构造函数
    Test t1(10);

    //2. 等号法:C++编译器调用有参构造函数
    Test t2 = (20, 10)

    //3. 构造函数法:手动直接调用构造函数
    Test t3 = Test(30);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/weicao1990/article/details/81630162