I create an instance of a class in the main function, how do I get access to this instance from a method of another class that is in another source code file and is included in the main function (which is then used in the same main function)?

Closed due to the fact that the essence of the question is not clear to the participants from Vlad from Moscow , Harry , Bald , user194374, Denis Jan 13 '17 at 6:46 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

    Pass it to the method of this class as a parameter.

    class Other { void method(const FromMain& m); ... int main() { FromMain a; Other b; ... b.method(a); 

    Does it interest you? If not, clarify your question, because without an exact TZ, the result of HZ ...

    Update
    It is possible - if it is known that the lifetime of the FromMain object exceeds the lifetime of the Other - to store the link directly in Other :

     class Other { Other(FromMain& M):m(M) {...} void method() { ... Работа с m ... } ... FromMain& m; int main() { FromMain a; Other b(a); ... b.method(); 
    • Yes, an interesting way, it fits but not exactly, because I actually have a large number of methods that should have access to this object and all of them transfer the link to the object in a similar way slightly difficult. I have been working in C ++ not so long ago, maybe there is some simpler way that I miss? - nammidd
    • Well, since you need to apply to it in any case, you will still have to specify it in the body of the method ... Well, take a look at the second option - I’ll add the answer now. - Harry
    • @nammidd In this case, you are probably comfortable with a global static variable. - user194374