#include "stdafx.h"
#include <iostream>
using namespace std;
int i=1;
/*数组构造*/
class testv1
{
public:
int v1;
testv1() {v1=i++;}
};
int _tmain(int argc, _TCHAR* argv[])
{
#define ARRAY_SIZE 10
//testv1 test_array[ARRAY_SIZE];//定义数组时自动调用构造函数,与JAVA不同
testv1 *test_array = new testv1[ARRAY_SIZE]();
for(int i=0;i<ARRAY_SIZE;i++)
{
test_array[i].testv1::testv1();//如果是test_array[i].testv1();编译器会报“error C2274: “函数样式转换”: 位于“.”运算符右边时非法”
cout<< "I am testv "<<test_array[i].v1<<endl;
}
delete[] test_array;
return 0;
}
显式调用构造函数
最新推荐文章于 2024-03-27 17:20:04 发布