【发布时间】:2015-10-20 11:44:06
【问题描述】:
我尝试创建一个接受并返回数组的类,但我遇到了一些问题。我不确定从类中返回数组是否合法。或者可以通过返回指向数组的指针来完成。感谢您对问题的任何解决方案。
#include <iostream>
using namespace std;
class myclass {
private:
int Array[10];
public:
myclass (int temp[10]) {
for (int i = 0; i < 10; i++) {
Array [i] = temp [i];
}
}
int returnArray () {
return Array; // error here, I'm not sure if it is legal to return an array.
}
int* returnArray2 () {
return this->Array; // hope it will return a pointer to the array
}
};
int main () {
int Array[10] = {1,2,3,4,5,6,7,8,9};
myclass A(Array);
cout << A.returnArray() << endl; // try to return an array and print it.
myclass* ptr = &A;
cout << *ptr->returnArray2 << endl; // error here
return 0;
}
【问题讨论】:
-
你打算用数组做什么?你的目标是什么?