How should I define this variable? - c++

I have several header files .h and their corresponding .cpp files. Here suppose I have part1.h and part2.h for declaration. The corresponding cpp are part1.cpp and part2.cpp for definition the functions.
I also have a file with main. In this main function, I have a variable float * change. As its name, I will change the value of change and then call function F defined in part1.cpp and G in part2.cpp. The problem is I cannot pass change as a parameter.
So at first I plan to define it as a global variable. But then I found this variable always changes. But a global variable only can be defined once. So is there any method to solve this problem?
Thank you in advance.

You can define the variable as normal in one of your .cpp files
float* change;
Then in one of your header files, you can declare it:
extern float* change;
Now #include the header file wherever the global variable is used.

I would recommend to do get/set functions to get variable from whenever you want and you'd keep OOP encapsulation. http://www.cplusplus.com/forum/beginner/107842/

Declare in one of your headers
extern float * change; // does not define the variable, but just that it exists somewhere
You can also declare this directly in part1.cpp and part2.cpp instead of a common header. However if you'd later change something, float to double for example, you should not forget any of those declarations.
Remember Stroustrups famous quote: "// global variable – avoid those where you can"

Related

How come you don't need to assign a value to an extern const (int) when including it in a header file? in c++

Usually when declaring a const of any sort you're supposed to assign a value to it immediately, but not in header files? why?
It's because you declared it extern, extern tells the compiler that the definition and declaration of the variable are in another file and you are simply making your code "aware" of the existence of that variable, as such the compiler does not expect any assignment on the header.
Remember not only you don't need to, but you shouldn't assign a value to extern declarations because that makes them a definition.

How to "hide" a variable inside the header file?

The header file contains two variables.
Because of the structure of my program, I have two "ld: duplicate symbol" errors.
These two variables have only local significance.
Is there any way of making these variables "private", so they wouldn't be seen outside of the header file even if the header file is included to another source file?
EDIT: please tell me, would it be nice if I will put the variables to the cpp file? These variables are very big arrays, defined while initializing, and take a lot of lines of code...
extern char Lookup[][3] = { "aa", "ab", "ac", "ad", "ae", "af", ... and so on (really long)}
The solution is to not define variables in your header file.
If you absolutely must share variables between internal source files (and I recommend that you don't), then you should do the following:
Create an "internal.h".
Declare your variable extern in that header file.
Include "internal.h" in both your internal source files.
Define the variable in one or other internal source files.
The variable is now hidden from the outside world. (It's probably still visible in your object files, but you can use platform-specific trickery to strip it.)
Do not define variable in headers.
Use extern to declare a variable in a header without defining it.
I'm always weary about variables which are "on the loose". I mean: they do affect something don't they? They "belong" to a class?
Shouldn't you just declare them under a class, and then declare them as static variables? (And given the syntax, probably constants too)? In that case you can simply use everything normally done with static variables (initializer lists, static initialize function etc). Seems to me much more clearer, as now your variable is tied with something.

extern pointer problem in c++

I have a header file that has a number of declarations like this:
extern ID3D10Device* device;
I can't make these static because of a problem with multiple translation units, and I can't have them as normal pointers for a similar reason. However when I try to build the program I get unresolved external symbol linker errors. I'm assuming that this is because I'm attempting to use the pointers without defining them first. This is the problem however, as the way you initialise these DirectX objects is by passing the address of the pointers as parameters into specialist methods. - I may be wrong but I am assuming this is the problem as the compiler / linker / whatever can't see the definitions.
All I'm trying to do is have these pointers (for the graphics device, depth buffer etc) visible to multiple classes. How can this be achieved?
You need the pointers to be defined in some translation unit. The linker is complaining because it seems you haven't done that anywhere. You should declare them at file scope as
ID3D10Device* device = NULL;
in the source file where you call the DirectX function that initializes them. Just make sure the declaration is only made in one source file, then the extern statement should be placed in the associated header file which is included by all translation units that need to use these pointers.
When you externally define a variable like this, the compiler is not reserving any memory for that variable until it sees a definition inside a code module itself. So if you are going to be passing these pointers by-reference to a function for initializing their values, they must be defined in a code module somewhere.
You only need to define them in a single code module ... then place the extern declarations inside a header file you include in the rest of your code modules that require access to the pointer variables. That shouldn't create any linker errors due to duplicate definitions.
I can't make these static because of a problem with multiple translation units
If you need a different variable in different TU (translation units), make it static: this way the variable will be specific to each TU.
A declaration of a variable is also definition unless the extern is used.
You must have one (and only one) definition of a variable in the program.
To have a global variable:
Declare it with the extern keyword in some header file.
Include this header in every TU that needs to use the variable. Never declare the variable directly, never bypass the header inclusion.
Define the variable: a declaration without the extern keyword will define the variable in one TU. You need to include the header file in the same TU to guaranty consistency between the extern declaration and the definition.
the way you initialise these DirectX objects is by passing the address
of the pointers as parameters into specialist methods
To solve this part of the problem, you could do something like this (in a .cpp file):
ID3D10Device* device;
struct Foo {
Foo(ID3D10Device **pdevice) { specialist_method(pdevice); }
};
Foo f(&device);
Beware of the "static initialization order fiasco", though -- it's safe to use device from main, or code called from main, because it will definitely be initialized before that code executes. It's not necessarily safe to use it from other initializers executed before main, because the order of initialization of statics in different translation units is unspecified (within a TU, they're initialized in order of either declaration or definition, I forget which). So device might still be a null pointer in that code. Likewise, specialist_method can't necessarily rely on other statics having been initialized.
There are extra tricks you can use if you need to enforce initialization orders, I'd guess that all the common ones are on SO already in other questions.

How to make large numbers of existing functions available in the scope of a class?

I need to make a large (100's of source files) project into a library, removing dozens of global variables by putting them all into a class object.
The problem is the thousand or so functions that now need to be members of this class so they have access to the object variables.
Otehr than adding MyClass:: to every single function definition in the source files, is there a way to cheat and indicate that all the functions in a particular source file should be part of the MyClass scope?
Add all the globals to a namespace.
// MyGlobals.h
namespace MyGlobals
{
extern int g_i;
extern double g_d;
extern A g_A;
}
Whatever files you want to access, do:
using namespace MyGlobals;
inside the header file. In this (using namespace) way you can indicate that all the variables should be accessible without using scope resolution :: for that file. (i.e. you can simply access g_i instead of MyGlobals::g_i inside that file).
Also note that, you have to define all the global variables inside a .cpp file:
// MyGlobals.cpp
#include "MyGlobals.h"
int MyGlobals::g_i;
double MyGlobals::g_d;
A MyGlobals::g_A;

C++ pass variable from .cpp to header file

this relates to a question i asked previously:
C++ array in header file
in the main.cpp file there is a variable called fin1
ifstream fin1("ACW2_data.txt");
this might be a stupid question, but how can i use the value of this variable from main.cpp in the header file? (ie. is there a way to pass variables between the two files?)
any other info about using header files may help
Thanks in advance
This variable can be declared in the header file as an extern.
extern ifstream fin1;
Now you can use this variable wherever you #include this header file including the header file itself. You don't need to pass the variable as such. :)
I think you need to back up and explain what you are trying to do. Header files are, in general, for defining common definitions and declarations.
What do you mean by "use the value in the header file"? In general, the header file is not where code is run. So what needs to use this variable there?
Generally speaking, variables that need to be used in more than one file should be declared in the header to begin with. In C++, this is normally in the form of class members.
Even more common is to pass variables as arguments when another function or method needs to use the same value.
I can't tell from the information you've provided, but it sounds like you're on the wrong track to me.
Declare this variable as extern .
extern ifstream fin1;
Than every time you change it it value gonna update and be ready in your header file
And you can include tour header every where and use that variable