-
December 10th, 2010, 09:31 AM
#1
Getting the value of a variable in another class
Hey codegurus,
I'm quite new to programming, and i have a small problem. I have an application where i want to declare a variable in a function, and then use the value from that variable in a function in a different class.
I have made a very simple example so it's easy for you to give feedback.
It's programmed in Qt (that's the reason for MainWindow, Widgets etc.)
I hope it's not too simple for you guys,
Thanks in advance.
Best regards,
-
December 10th, 2010, 09:38 AM
#2
Re: Getting the value of a variable in another class
Originally Posted by 99999
I'm quite new to programming, and i have a small problem. I have an application where i want to declare a variable in a function, and then use the value from that variable in a function in a different class.
I have made a very simple example so it's easy for you to give feedback.
It's programmed in Qt (that's the reason for MainWindow, Widgets etc.)
If you don't know how to pass a simple value from one class to another and you are programming GUI application, you are in over your head.
You can't return a value from a function if you declare the return type as void. Declare the return type as int instead and you can store the value when calling the function.
Code:
int foo()
{
return 12;
}
void bar()
{
int x = foo();
// do something with x
}
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
December 10th, 2010, 09:46 AM
#3
Re: Getting the value of a variable in another class
Thanks for a quick reply.
But, my function calls a gps to get a longtitude and a latitude where it puts the values into two variables. I want to be able to use both of these values in another function in another class.
How do i do that? As i see it i can't just make an int and return two values.
-
December 10th, 2010, 09:58 AM
#4
Re: Getting the value of a variable in another class
Talking about "another class" is beside the point. If you need to use state from one object inside an operation for a different object----whether the two objects are of the same class or not----then you (a) need to pass a copy, reference, const reference, or pointer to the object with the relevant state into the method which needs it, and (b) the object with the relevant state must have an accessor method to get at the relevant state, or that state must be public. (In the special case when the two objects are of the same type, you can get at private state directly. However it's still often a good idea to use the public interface if it's available.)
Last edited by Lindley; December 10th, 2010 at 10:02 AM.
-
December 10th, 2010, 10:32 AM
#5
Re: Getting the value of a variable in another class
Hey, i just tried something which compiled. It just didn't get the value returned from "me".
If i want to use 2 values from 2 variables from another function how do i do that.
As an example, what is wrong with the code in the picture if i want to be able to use the values from x and from y in the function meto.
Im very thankfull you take the time to help a noob like me on the right way!
-
December 10th, 2010, 10:38 AM
#6
Re: Getting the value of a variable in another class
Why not just make x and y members of the MainWindow class, rather than having them local to me()?
-
December 10th, 2010, 10:48 AM
#7
Re: Getting the value of a variable in another class
Originally Posted by Lindley
Why not just make x and y members of the MainWindow class, rather than having them local to me()?
Hey, good idea. But even if i did that, how would i return the two values they get in me and and use them in meto?
I believe i still have to make an object, but when i make an object i believe i can't just set int y = obj since me returns two variables. So, lets say that in the function me, x and y gets the value 10 and 12. How do i get these values into meto and assign them to two new variables?
-
December 10th, 2010, 10:59 AM
#8
Re: Getting the value of a variable in another class
Originally Posted by 99999
Hey, good idea. But even if i did that, how would i return the two values they get in me and and use them in meto?
I believe i still have to make an object, but when i make an object i believe i can't just set int y = obj since me returns two variables. So, lets say that in the function me, x and y gets the value 10 and 12. How do i get these values into meto and assign them to two new variables?
Both me() and meto() are methods of a MainWindow object. If you assign values to variable members of the object in me(), then those members will of course still be available in meto(). No return is required. The only danger is that you're imposing a dependency between the two methods; you need to carefully design your program so that calling them in the wrong order won't crash your code or cause other undesirable behavior.
-
December 10th, 2010, 11:14 AM
#9
Re: Getting the value of a variable in another class
Originally Posted by 99999
Hey, i just tried something which compiled. It just didn't get the value returned from "me".
If i want to use 2 values from 2 variables from another function how do i do that.
As an example, what is wrong with the code in the picture if i want to be able to use the values from x and from y in the function meto.
Im very thankfull you take the time to help a noob like me on the right way!
Once you hit the first return statement the function is done. You can't get to the second one. If you need to return two variables, one option is to use output parameters.
Output parameters are arguments that are passed into a function by the calling function and populated by the called function. They have to be passed in by reference.
Code:
void FunctionWithOutputParameters(int& Param1, int&Param2)
{
Param1 = 5;
Param2 = 6;
}
//then call to call it
int n1, n2;
FunctionWithOutputParameters(n1, n2);
-
December 11th, 2010, 09:35 AM
#10
Re: Getting the value of a variable in another class
Originally Posted by GCDEF
Output parameters are arguments that are passed into a function by the calling function and populated by the called function. They have to be passed in by reference.
Code:
void FunctionWithOutputParameters(int& Param1, int&Param2)
{
Param1 = 5;
Param2 = 6;
}
//then call to call it
int n1, n2;
FunctionWithOutputParameters(n1, n2);
Perfect, this is exactly what i needed!!! Now my app is up running
Thanks again!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|