CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2009
    Posts
    9

    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 ??

  2. #2
    Join Date
    Dec 2009
    Posts
    3

    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.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: acces local variable

    Quote Originally Posted by syac View Post
    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.

    or any suggestion ??
    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

  4. #4
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    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.

  5. #5
    Join Date
    Aug 2009
    Posts
    68

    Re: acces local variable

    i got same problem..
    Last edited by auliac; December 18th, 2009 at 01:29 PM.

  6. #6
    Join Date
    Dec 2009
    Posts
    9

    Re: acces local variable

    Quote Originally Posted by Cmorhead View Post
    My first question would be why?
    Quote Originally Posted by Paul McKenzie View Post
    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?

  7. #7
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    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.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: acces local variable

    Quote Originally Posted by syac View Post
    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.

  9. #9
    Join Date
    Aug 2009
    Posts
    68

    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
  •  





Click Here to Expand Forum to Full Width

Featured