c++ 对象数组
#include <iostream>
class A
{
public:
int num;
A(){
std::cout<<"A()"<<'\n';};
A(int n){
this->num =n;
std::cout<<"A(int)"<<'\n';};
~A(){
std::cout<<"~A()";};
void sayWord(){
std::cout<<this->num<<'\n';
}
};
int main(int argc, const char * argv[])
{
// insert code here...
你可以这样
A a[5];
for (int i=0; i<5; i++) {
a[i].sayWord();
}
你可以这样
A aa[5]={A(1),A(2),A(3),A(4),A(5)};
for (int i=0; i<5; i++) {
aa[i].sayWord();
}
你可以这样
A * aaa[5];
for (int i=0; i<5; i++) {
aaa[i]=new A(i);
}
for (int i=0; i<5; i++) {
aaa[i]->sayWord();
}
for (int i=0; i<5; i++) {
delete aaa[i];
}
return 0;
}
补充:软件开发 , C++ ,