Simple questions for the experts
-
Hi everyone, I wanted to know if someone could help me. I have a project with three different classes for three different dialogs(3 .cpp files). I have a variable that the user enters, which is in one of the classes (dialogs). I want to use that same variable with the stored information in my other dialog(class). How do I do that? Someone told me to pass a pointer, but i don't know how to exactly do that cause i get some error when i run the program. Thanks for helping me. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
-
Hi everyone, I wanted to know if someone could help me. I have a project with three different classes for three different dialogs(3 .cpp files). I have a variable that the user enters, which is in one of the classes (dialogs). I want to use that same variable with the stored information in my other dialog(class). How do I do that? Someone told me to pass a pointer, but i don't know how to exactly do that cause i get some error when i run the program. Thanks for helping me. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
- You can declare your variable in global scope. 2) You can use pointer to the variable in the first class in the last two classes. yiy
-
- You can declare your variable in global scope. 2) You can use pointer to the variable in the first class in the last two classes. yiy
How would I use a pointer to the variable in the class so that the other classes could use it? Can you help me with this. I am new to programming, and am trying to teach myself with a book. (i'm only 15) Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
-
How would I use a pointer to the variable in the class so that the other classes could use it? Can you help me with this. I am new to programming, and am trying to teach myself with a book. (i'm only 15) Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
I think, that the better way to do this is declare m_var as static variable. After that you can use this variable as CL1::m_var. ------------------------------------------------------------ class CL1 { public: CL1(); //constructor virtual ~CL1(); //destructor public: static int m_var; //your variable public: void SetVariable(int value) }; CL1::m_var=0; //linking will fail without this. CL1::Cl1() { } CL1::~CL1() { } void CL1::SetVariable(int value) { m_var=value; } #include using namespace std; int main() { CL1 myclass; int user_val; cin>>user_val; myclass.SetVariable(user_val); cout<<"After input "<
-
Hi everyone, I wanted to know if someone could help me. I have a project with three different classes for three different dialogs(3 .cpp files). I have a variable that the user enters, which is in one of the classes (dialogs). I want to use that same variable with the stored information in my other dialog(class). How do I do that? Someone told me to pass a pointer, but i don't know how to exactly do that cause i get some error when i run the program. Thanks for helping me. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
Hello, the following should work: In the dialog class add the following:
private:
VarType m_Var;
public:
VarType GetVar() {return m_Var;}In the other classes at a point where you want to use the var add
// can only appear in functions and constructors
VarType var = YourClass.GetVar();this should do the trick! Good luck! :)
A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.