initialization of member variables
-
hi, i've 2 classes ....... class1 { public: CString str1; get(); put(); } class2 { public: CString str2; class2(CString str) { str2 = str; } } when i'm calling constructor of class2 in put function of class1 str2 gets set ... but when i access str2 in a function of class2, it is blank .... can't we use a constructor to set values of members in this way? Thanks, Kranti
-
hi, i've 2 classes ....... class1 { public: CString str1; get(); put(); } class2 { public: CString str2; class2(CString str) { str2 = str; } } when i'm calling constructor of class2 in put function of class1 str2 gets set ... but when i access str2 in a function of class2, it is blank .... can't we use a constructor to set values of members in this way? Thanks, Kranti
What you are actually doing is to construct an object of class2 in class1. class1's object is initializing the class2's objects's variable to a certain value. But any more objects of class2 which you create outside will not be affected by that. Unless you are using a hierarchy, like class1 is derived from class2. So now you could create an object of class1, and it will contain all non-private members of class2 as well. this is this.
-
hi, i've 2 classes ....... class1 { public: CString str1; get(); put(); } class2 { public: CString str2; class2(CString str) { str2 = str; } } when i'm calling constructor of class2 in put function of class1 str2 gets set ... but when i access str2 in a function of class2, it is blank .... can't we use a constructor to set values of members in this way? Thanks, Kranti
-
Kranti1251984 wrote:
but when i access str2 in a function of class2, it is blank ....
Where is the object of class2 created? Are you using the same class2 object to access the member function? Vini
object of class1 is being created inside a function of class2 to set the value of a member of class1 thru' a parameterized constructor of class2 as class1 { public: CString str1; get(); { //get str1 } put() { class2 c2(str1); } } class2 { public: CString str2; class2(CString str) { str2 = str; } } Thanks, Kranti
-
object of class1 is being created inside a function of class2 to set the value of a member of class1 thru' a parameterized constructor of class2 as class1 { public: CString str1; get(); { //get str1 } put() { class2 c2(str1); } } class2 { public: CString str2; class2(CString str) { str2 = str; } } Thanks, Kranti
-
object of class1 is being created inside a function of class2 to set the value of a member of class1 thru' a parameterized constructor of class2 as class1 { public: CString str1; get(); { //get str1 } put() { class2 c2(str1); } } class2 { public: CString str2; class2(CString str) { str2 = str; } } Thanks, Kranti
-
i wanted to use str2 thruough out. It's ok now, solved it by using a member function to set the value in class1. Thank you all! Kranti
-
Kranti1251984 wrote:
put() { class2 c2(str1); }
If you are using the same c2 object to access the member function, you will get the value of the variable str2. The object c2 is destroyed once you exit the
put()
function of class1. Vini