Change A renew()
to void renew()
and the code works.
#include<iostream>
using namespace std;
class A{
private:
int **m1;
int **m2;
void allocate_mem(int ***ptr){
*ptr = new int*[1];
(*ptr)[0] = new int[1];
}
public:
A(){
allocate_mem(&m1);
m1[0][0] = 1;
allocate_mem(&m2);
m2[0][0] = 1;
}
//I need a method that change m2 (according to the value of m1) without changhig the value of m1
void renew(){
if(m1[0][0]>0)
m2[0][0] = 1000;
}
~A(){
delete[] m1[0];
delete[] m1;
delete[] m2[0];
delete[] m2;
}
void create_output(){
cout << m1[0][0] << endl << m2[0][0] << endl;
}
};
int main(){
A mat;
mat.create_output();
mat.renew();
mat.create_output();
return 0;
}
This produces:
1
1
1
1000
solved c++ class two dynamic attributes [closed]