Jump to content
  • Advertisement
Sign in to follow this  
baker

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

This topic is 4487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!