#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; } } 
  • Immediately do CArrayData Cl(n, new Mytype[n]); . But in general, take better std::vector , and then with new/delete too easy to shoot yourself a leg. (For example, your class does not follow the rule of three , so something like CArrayData C1(n, pm), C2(C1); will lead to undefined behavior due to double calling delete on the same pointer.) - HolyBlackCat
  • @HolyBlackCat How can I initialize elements in the case of 'CArrayData Cl (n, new Mytype [n]);' ? - Default Defoltovich
  • Do it inside the classroom? This is what I do not understand: By what criteria do you want to determine whether it is possible to change the data? Before you can, and after - no? - HolyBlackCat
  • @HolyBlackCat I want to prohibit changing the array from main - Default Defoltovich
  • And what, pm[i] = Mytype (rand() % 1000)/ 10; - not a change? - HolyBlackCat pm

2 answers 2

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; } }