|
-
December 18th, 2009, 12:12 PM
#1
acces local variable
hello friend....
if i wanto to acces local variable in other function in same class, how can i?? example : i have function A, B in same class. i want to acces values variable in class A from function B. i tried using pointer, but it did not work. this my code:
Code:
#include <iostream>
using namespace std;
int* akses_a1;
void A()
{
int a1 = 2;
int a2 =3;
akses_a1 = &a1;
}
void B()
{
cout << *akses_a1 <<endl;
}
int main()
{
B();
return 0;
or any suggestion ??
-
December 18th, 2009, 12:20 PM
#2
Re: acces local variable
My first question would be why?
Why don't you just put the local variable, a1 in your example, and put it as a member of the class?
The problem that you're having is that the variable you want is being allocated on function entry and being removed on exit. Even if you save the address of the variable it will be memory garbage after the function has exited.
Since it seems that your example is in procedural C, the best way to get your example to work would be to make "akses_a1" and regular int and call A() either before B() in main, or in B() before the cout.
I hope that helps.
-
December 18th, 2009, 01:04 PM
#3
Re: acces local variable
 Originally Posted by syac
hello friend....
if i wanto to acces local variable in other function in same class, how can i??
You can't. Once the function returns, those local variables disappear.
Local variables are to be used local to the function. If you want to access variables from other functions, then you can't make them local. Either create a class with these variables, or use global variables.
Code:
#include <iostream>
using namespace std;
int akses_a1;
int A()
{
int a1 = 2;
int a2 =3;
return a1; // return value of a1 here
}
void B()
{
akses_a1 = A();
cout << akses_a1 <<endl;
}
int main()
{
B();
return 0;
}
Regards,
Paul McKenzie
-
December 18th, 2009, 01:23 PM
#4
Re: acces local variable
Hmm, why do you think local variables are called "local" ?
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
December 18th, 2009, 01:27 PM
#5
Re: acces local variable
Last edited by auliac; December 18th, 2009 at 01:29 PM.
-
December 18th, 2009, 01:30 PM
#6
Re: acces local variable
 Originally Posted by Cmorhead
My first question would be why?
 Originally Posted by Paul McKenzie
int A()
that code just example of my problem. my true code is very long code. if i call function, it will do any thing that coded in that function. and the function's tipe in my true code is void. so i use same example in my post. any suggestion again?? what must i do?
-
December 18th, 2009, 01:38 PM
#7
Re: acces local variable
As treuss have very well explained, local objects are... well, local. (Actually, you can use their addresses in other places, but you must be aware of their life-cycle.) So you really can't do that in the way you want. One option similar to your example would be to use a non-local object created on the free store. In this case, don't forget to delete it.
Code:
#include <iostream>
using namespace std;
int* akses_a1;
void A()
{
int * a1 = new int(2);
akses_a1 = a1;
}
void B()
{
cout << *akses_a1 <<endl;
delete akses_a1;
}
int main()
{
A(); //To create the int.
B();
return 0;
}
Last edited by ltcmelo; December 18th, 2009 at 01:41 PM.
-
December 18th, 2009, 01:58 PM
#8
Re: acces local variable
 Originally Posted by syac
that code just example of my problem. my true code is very long code. if i call function, it will do any thing that coded in that function. and the function's tipe in my true code is void. so i use same example in my post. any suggestion again?? what must i do?
What other restrictions should we all know about? You can't use local variables, so start with that fact first. What I gave you is one solution. Here is another.
Code:
#include <iostream>
using namespace std;
int akses_a1;
void A(int *varToChange)
{
int a1 = 2;
int a2 =3;
*varToChange = a2;
}
void B()
{
A(&akses_a1);
cout << akses_a1 <<endl;
}
int main()
{
B();
return 0;
}
Regards,
Paul McKenzie
Last edited by Paul McKenzie; December 20th, 2009 at 11:56 AM.
-
December 20th, 2009, 10:22 AM
#9
Re: acces local variable
okay,,, thanks all,,, i will try to use one of methods that you explained...
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
|