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

Started by
3 comments, last by baker 18 years, 1 month ago
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
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.
so is using externs usually a bad practice?
Well global variables in general aren't the greatest thing in the world.
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.

This topic is closed to new replies.

Advertisement