对象数组

#include "pch.h"
#include <iostream>
using namespace std;
class CTest
{
public:
    CTest()
    {
        cout << "CTest() " << endl;
    }
    CTest(int a)
    {
        m_age = a;
        cout << "CTest(int a) " << a << endl;
    }
private:
    int m_age;
};

int main()
{
    CTest cT1[3];
    // 定义对象数组,同时调用带参数的构造函数  
    CTest cT2[3] = { CTest(1),CTest(2),CTest(3) };
    std::cout << "Hello World!
"; 
}

输出:

CTest()
CTest()
CTest()
CTest(int a) 1
CTest(int a) 2
CTest(int a) 3
Hello World!
原文地址:https://www.cnblogs.com/xiangtingshen/p/11510162.html