今天的小目标 : 先来一个亿,咳咳咳.实现:new一个数组,成员全部用1初始化:
int * p = new int[10](1);
应该是编译不过的哈!.
再来:
int * p = new int[10];
for(int i = 0; i < 10; i++) { p[i] = 1;}
嗯,,,,,可以,但是不推荐.
struct A {
public:
int data;
A(int init = -1) :data(init){
printf("A()\n");
}
};
int main() {
/*自定义类*/
A* tab1 = new A[10]();//调用构造函数,使用默认参数
for (int i = 0; i < 10; i++)
{
printf("%d ", *tab1++);
}
printf("\n");
A* tab = new A[10]{1,2,3,4,5,6,7,8,9,-1};
for (int i = 0; i < 10; i++)
{
printf("%d ",*tab++);
}
printf("\n");
/*内建类*/
int * tab_int0 = new int[10]();
for (int i = 0; i < 10; i++)
{
printf("%d ", *tab_int0++);
}
printf("\n");
int * tab_int = new int[10]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int i = 0; i < 10; i++)
{
printf("%d ", *tab_int++);
}
printf("\n");
return 0;
}
标签:10,初始化,int,c++,++,tab,printf,new
From: https://www.cnblogs.com/jlh-/p/16848506.html