How to access variables defined and declared in one function in another function?

后端 未结 4 1906
庸人自扰
庸人自扰 2020-12-05 11:05

Can anyone tell me how to access variables declared and defined in a function in another function. E.g

void function1()
{
   string abc;
}

void function2()
         


        
相关标签:
4条回答
  • 2020-12-05 11:39

    If you have a variable in function1 that you want to use in function2, then you must either:

    1. pass it directly,
    2. have a higher scope function that calls both declare the variable and pass it, or
    3. declare it a global and then all functions can access it

    If your function2 is called from function1, then you can just pass it as an argument to function2.

    void function1()  
    {  
        std::string abc;  
        function2( abc );  
    }  
    
    void function2( std::string &passed )   
    {   
        // function1::abc is now aliased as passed and available for general usage.
       cout << passed << " is from function1.";   
    }   
    

    If function1 doesn't call function2, but both are called by function3, then have function3 declare the variable and pass it to both function1 and function2 as an argument.

    void parentFunction( )
    {
        std::string abc;  
        function1( abc );  
        function2( abc );  
    }
    void function1( std::string &passed )   
    {   
       // Parent function's variable abc is now aliased as passed and available for general usage.
       cout << passed << " is from parent function.";   
    }   
    void function2( std::string &passed )   
    {   
        // Parent function's variable abc is now aliased as passed and available for general usage.
       cout << passed << " is from parent function.";   
    }    
    

    Finally, if neither function1 nor function2 is called from each other, nor from the same function in code, then declare the variable to be shared as a global, and function1 and function2 will be able to directly use it.

    std::string global_abc;  
    void function1( )   
    {   
       cout << global_abc << " is available everywhere.";   
    }   
    void function2( )   
    {   
       cout << global_abc << " is available everywhere.";   
    }    
    
    0 讨论(0)
  • 2020-12-05 12:00

    There is absolutely no way. Variables of the block can be directly accessed ONLY from that block.

    Pointers/references to the object can be passed into functions that are called from this block as parameters.

    0 讨论(0)
  • 2020-12-05 12:01

    The C++ way is to pass abc by reference to your function:

    void function1()
    {
        std::string abc;
        function2(abc);
    }
    void function2(std::string &passed)
    {
        passed = "new string";
    }
    

    You may also pass your string as a pointer and dereference it in function2. This is more the C-style way of doing things and is not as safe (e.g. a NULL pointer could be passed in, and without good error checking it will cause undefined behavior or crashes.

    void function1()
    {
        std::string abc;
        function2(&abc);
    }
    void function2(std::string *passed)
    {
        *passed = "new string";
    }
    
    0 讨论(0)
  • 2020-12-05 12:05

    Make it global then both can manipulate it.

    string abc;
    
    void function1(){
        abc = "blah";
    } 
    
    void function2(){
        abc = "hello";
    } 
    
    0 讨论(0)
提交回复
热议问题