#include <iostream> using namespace std; typedef int Mytype; class CArrayData { private: int size; Mytype* array; public: CArrayData(int size, Mytype* array) // ΠΊΠΎΠ½ΡΡΡΡΠΊΡΠΎΡ ΠΊΠ»Π°ΡΡΠ° { this->array = array; this->size = size; } CArrayData::~CArrayData() { // Π΄Π΅ΡΡΡΡΠΊΡΠΎΡ delete[] array; size = 0; } friend void out(CArrayData &CArray); }; int main(int argc, char** argv) { setlocale(LC_ALL, "russian"); int n; cout << "ΠΠ²Π΅Π΄ΠΈΡΠ΅ ΡΠ°Π·ΠΌΠ΅Ρ ΠΌΠ°ΡΡΠΈΠ²Π°: " << endl; cin >> n; Mytype *pm = new Mytype[n]; CArrayData Cl(n, pm); for (int i = 0; i < n; ++i) { pm[i] = Mytype (rand() % 1000)/ 10; } out(Cl); pm[0] = 100500; //Π·Π΄Π΅ΡΡ ΠΏΠ΅ΡΠ²ΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΈΠ·ΠΌΠ΅Π½ΡΠ΅ΡΡΡ Π½Π° 100500, ΠΊΠ°ΠΊ ΡΡΠΎ Π·Π°ΠΏΡΠ΅ΡΠΈΡΡ? out(Cl); cin.get(); cin.get(); return 0; } void out(CArrayData &CArray) { for (int i = 0; i < CArray.size; ++i) { cout << CArray.array[i] << endl; } } |
2 answers
One of the options is to introduce changes in the class (at the same time we will correct errors).
class CArrayData { const int size; //ΡΠ°Π·ΠΌΠ΅Ρ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π±ΡΡΡ ΠΊΠΎΡΡΠ°Π½ΡΠ½ΡΠΌ const Mytype* array; // Π΄Π»Ρ Ρ
ΡΠ°Π½Π΅Π½ΠΈΡ Π°Π΄ΡΠ΅ΡΠ° ΠΊΠΎΠ½ΡΡΠ°Π½ΡΠ½ΠΎΠ³ΠΎ ΠΎΠ±ΡΠ΅ΠΊΡΠ° public: // ΠΊΠΎΠ½ΡΡΠ°Π½ΡΠ½ΡΠ΅ ΠΎΠ±ΡΠ΅ΠΊΡΡ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·ΠΈΡΡΡΡΡΡ ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΎΡΠΎΠΌ ΠΊΠΎΠ½ΡΡΡΡΠΊΡΠΎΡΠ° CArrayData(const int size, const Mytype* array) : size(size), array(array) {}// ΠΊΠΎΠ½ΡΡΡΡΠΊΡΠΎΡ ΠΊΠ»Π°ΡΡΠ° ~CArrayData() { // ΡΡΡ Ρ Π²Π°Ρ Π±ΡΠ»Π° ΠΎΡΠΈΠ±ΠΊΠ° Π΄ΠΎΠΏΠΎΠ»ΡΠ½ΠΈΡΠ΅Π»ΡΠ½Π°Ρ ΠΊΠ²Π°Π»ΠΈΡΠΈΠΊΠ°ΡΠΈΡ delete[] array; } //... }; In the program, we first initialize the array, and then assign its address to a constant pointer: ( Edit : pointer to a constant object)
Mytype *p = new Mytype[n]; for (int i = 0; i < n; ++i) { p[i] = Mytype (rand() % 1000)/ 10; } const Mytype* pm = p; CArrayData Cl(n, pm); pm[0] = 100500; //Π½Π΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΠΈΠ·ΠΌΠ΅Π½ΡΡΡ - Thank you so much! - Default Defoltovich
- Please, but in the last sentence requires editing - AR Hovsepyan
|
Everything turned out to be simpler, just transferred the initialization of the array to the constructor, it seems that more elements do not change
#include <iostream> using namespace std; typedef int Mytype; class CArrayData { int size; Mytype* array; public: CArrayData(int size, Mytype* array) // ΠΊΠΎΠ½ΡΡΡΡΠΊΡΠΎΡ ΠΊΠ»Π°ΡΡΠ° { cout << "Constructor\n"; this->size = size; this->array = new Mytype[size]; for (int i = 0; i < size; ++i) { this->array[i] = (rand()%1000)/10; } } ~CArrayData() { cout << "Destructor\n"; delete[] array; } //-------------------------------------------------------------------------- friend void out(CArrayData &CArray); }; int main(int argc, char** argv) { setlocale(LC_ALL, "russian"); int n; cout << "ΠΠ²Π΅Π΄ΠΈΡΠ΅ ΡΠ°Π·ΠΌΠ΅Ρ ΠΌΠ°ΡΡΠΈΠ²Π°: " << endl; cin >> n; Mytype *pm = new Mytype[n]; CArrayData Cl(n, pm); out(Cl); pm[0] = 100500; out(Cl); cin.get(); cin.get(); return 0; } void out(CArrayData &CArray) { for (int i = 0; i < CArray.size; ++i) { cout << CArray.array[i] << endl; } } |
CArrayData Cl(n, new Mytype[n]);. But in general, take betterstd::vector, and then withnew/deletetoo easy to shoot yourself a leg. (For example, your class does not follow the rule of three , so something likeCArrayData C1(n, pm), C2(C1);will lead to undefined behavior due to double callingdeleteon the same pointer.) - HolyBlackCatpm[i] = Mytype (rand() % 1000)/ 10;- not a change? - HolyBlackCat pm