题解 | #数组类的构造函数#
数组类的构造函数
https://www.nowcoder.com/practice/1f36f85726474afbb103f65a24106824
#include <iostream> using namespace std; class Nums{ public: int n; int *a; Nums(int n, int *a){ this->n = n; this->a = new int[this->n]; for(int i=0; i<this->n; i++){ this->a[i] = a[i]; } } ~Nums(){ delete[] this->a; } }; int main() { int n; cin >> n; int *p = new int[n]; for(int i=0; i<n; i++){ int item; cin >> item; p[i] = item; } Nums nums(n, p); for(int i=0; i<nums.n; i++){ cout << nums.a[i] << " "; } delete[] p; }