对象数组

 

#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!\n"; 
}

输出:

CTest()
CTest()
CTest()
CTest(int a) 1
CTest(int a) 2
CTest(int a) 3
Hello World!

 

posted @ 2019-09-12 09:31  狂奔~  阅读(168)  评论(0编辑  收藏  举报