c++ - Writing to variable within a class of another class -


i'm trying write variables 1 , 2 in class3 class1.

i don't have problem writing variable in class2 class1. however, can't seem permanently write class3 variable class1. know they're receiving right values because cout within read() method, after assignments, print correct values. however, same cout pasted print() method not print new values. seems though getting temporarily written...

sorry class madness

class class1 { public:   class1(); private:   class2 *myclass2array; };  class1::class1() {   myclass2array = new class2[size];   myclass2array[i].getarray(myclass2array[i].getcount()).read(string1, string2); // line problem } 

so, constructor class1 trying call read() that's in class3, declared in class2...

class class2 { public:   class2();   int getcount();   class3 getarray(int i) { return myclass3array[i]; } private:   class3 *myclass3array;   int count; };   class2::class2() {   count = 0;   myclass3array = new class3[8]; } 

i've tried class3 myclass3array[8]; , initializing each 1 in loop... here's class3

class class3 {   string var1;   string var2; public:   class3();   void print();   void read(string, string); };    class3::class3() {   var1 = "";   var2 = ""; }  void class3::print() {   cout << var1 << " , " << var2 << endl; // print old, initiated values } // end function print()  void class3::read(string string1, string string2) {   var1 = string1;   var2 = string2;   cout << var1 << " " << var2 << endl; // print new values } // end function read() 

i'm guess problem either in way declared array, or in how i'm trying access (write to) it. ideas?

the problem class2::getarray method returning copy.

change return reference

class3& getarray(int i) { return myclass3array[i]; } 

the problem setting values of copy of class3 object, not on original in class2 object. that's why values don't stick.

another way add setarray method

void setarray(int i, const class3& a) { myclass3array[i] = a; } 

then

class3 c3; c3.read(string1, string2); myclass2array[i].setarray(myclass2array[i].getcount(), c3); 

Comments