Am I supposed to put all my code in one cpp file, or can I spread them out over several files?
If I can spread them out, mind telling me how? I'm using VC++.
Printable View
Am I supposed to put all my code in one cpp file, or can I spread them out over several files?
If I can spread them out, mind telling me how? I'm using VC++.
Just create new C++ files, and use the extern keyword to reference things in other source files, ex.... I think. I'm really not all that good with C++.Code:extern int myfunc(int, int, long int);
You don't even need the extern keyword.
Just add new .cpp files to the project and make sure you have function prototypes when you need them and no name conflicts.
Cool, thanks:)
Wow, then I must've read my book wrong, because it says you always have to use it. When do you use it, then? Only for variables?
Yes. Functions are extern by default, as are variables, but you need to the extern keyword for variables to distinguish between a declaration and a definition:
The alternative is to mark things as static, but this has a very different meaning. Don't do it unless you know what it means.Code:// function declaration: no code block
// is extern by default
// there may be as many as you want that
// look the same as each one is only a hint
// for the compiler
void func(void);
// function definition: has a code block
// is extern by default
// only one in the whole code, else the linker
// won't be able to decide which one to use
void func(void)
{
exit(0);
}
// variables don't have code blocks
// therefore this is a definition
// extern linkage by default
// only one allowed in the whole code
int global_integer;
// to get a pure declaration you have to
// add the extern keyword
// all variables declared in header files
// need this keyword
extern int global_integer;
I believe I do know what static means (even after the scope it's in has left, it is still initialised and has all the data in it?)
static has 3 different meanings in C++, what you posted is just one of them.
I'll be back and post something about it.
The 3 meanings of static
This is a prepared post ready for reposting and contains no contextual references to any thread.
Meaning 1:
calls is a static local variable: it has the lifetime of a global variable and is initialized only once but can only be accessed from within this function.Code:void count_calls(void)
{
static int calls = 0;
calls++;
}
Meaning 2:
Defining two variables or functions with the same name in two different source files would normally lead to linker errors: references to the names cannot be properly resolved because of ambiguity. The static here means that the identifier has internal linkage, which means that the linker will not try to resolve references from other source files to this identifier. This means that the name can only be used within this file, but it also means that other files may use static identifiers with the same name.Code:onefile.cpp
static int some_variable;
static void some_function(void)
{
// do something
}
anotherfile.cpp
static int some_variable;
static void some_function(void)
{
// do something
}
Meaning 3 (C++ only):
Here the members marked static are class members instead of instance members. You don't need an instance of the class to access them and they exist only once. They are shared between all instances of the class. Access from outside is usually done with the normal scope resolution operator:Code:class myclass
{
static int num_instances;
public:
static int get_num_instances() {
return num_instances;
}
myclass() {
++num_instances;
}
~myclass() {
--num_instances;
}
};
int myclass::num_instances = 0;
int inst = myclass::get_num_instances();