Sign in to follow this  
baker

do i use extern if i need to access my local db in another cpp file?

Recommended Posts

baker    158
hi. i have basic question. please see below where i put the !!!!! //myObj.hpp class myObj { data functions etc... } //myObj.cpp myObj::etc... //myMain.cpp main { map<int, myObj> myLocalData; } //somethingElse.hpp class somethingElse { data... memberfunctions... } //somethingElse.cpp somethingElse::somefuntion { access whats in my myLocalData Map; !!!!!!!!!!!!!!!!!!!!!! } now how can i accesss myLocalData in another .cpp file? Do i use "extern map<int, myObj> myLocalData" in the somethingElse.cpp? Thanks

Share this post


Link to post
Share on other sites
SiCrane    11839
If you want to access a local variable in another function you'll need to pass a pointer or reference to the variable as an argument to the function. You can also make the variable a global variable by removing it from the function definition and externing it in a header as normal for global variables.

Share this post


Link to post
Share on other sites
baker    158
yea, ive been reading a c++ book and it does state that passing by reference is a better practice since externs are more prone to errors and such.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this