Class Problem?
-
//A.h Class A { A(int i, int j); int GetIJ(); int K,L; } //A.cpp { A::A(int i, int j) { K=i; L=j; } A::GetIJ() { int M=K+L; return M; } } //B.cpp A *ATemp; { void B::FirstFunction() { int a=5; int b=6; A aTest(a,b); //Create a A Instance "aTest" ATemp=&aTest; //assign aTest to ATemp } void B::SecondFunction() { int XYZ=ATemp->GetIJ(); //Is this correct? If this is wrong please tell me why } } ************** Can I Create a Object in one function and using the object at another function?
-
//A.h Class A { A(int i, int j); int GetIJ(); int K,L; } //A.cpp { A::A(int i, int j) { K=i; L=j; } A::GetIJ() { int M=K+L; return M; } } //B.cpp A *ATemp; { void B::FirstFunction() { int a=5; int b=6; A aTest(a,b); //Create a A Instance "aTest" ATemp=&aTest; //assign aTest to ATemp } void B::SecondFunction() { int XYZ=ATemp->GetIJ(); //Is this correct? If this is wrong please tell me why } } ************** Can I Create a Object in one function and using the object at another function?
Not this way. You have created the object on the stack in the first function and so the object is destroyed when the function returns. Therefore, your pointer to this object is no longer valid outside the scope of the first function. There are lots of ways to approach this problem, but one is to return an object of type A by value from the first function and assign it to another object of type A which is declared outside the function. The best method depends on the cost of creating and assigning the object.