I'm not an expert on this subject but here is how I understand it. You should probably make sure other posters confirm my suspicions before you trust them.
You get different kinds of declarations and definitions, to name a few: structure declarations/definitions, function declarations/definitions, variable declarations/definitions. In general if some construct in the code tells you everything you need to know about some entity, then it's a definition; if the construct leaves out some information that will be given ("defined") somewhere else then it's a declaration.
So starting with the simplest example, consider functions. If I write
Code:
void f() {
std::cout << "Inside f()\n";
}
then that's a function definition. It tells you everything you need to know about the function: what type it returns, what parameters it takes, what its code is. But if I want to call the function f() from a different file, or higher up in the same file (before it was defined), then I'll need a function prototype. Right?
This is a declaration, because it tells you enough information about f() to be able to use it, but not everything. Clearly, we can't tell from this what f() actually does. The declaration is saying to the compiler, "Somewhere there's a function that looks like this. I promise I'll give you the code for it at some point."
You have something similar with different kinds of entities. Consider structs/classes. When I say
Code:
struct S {
public:
int x;
};
then that's a structure definition because it tells you absolutely everything about the structure. However, C++ allows me to try to write mutually recursive data structures (as long as I use pointers instead of straight variables), e.g.
Code:
struct A {
B *b; // error here
};
struct B {
A *a;
};
This code won't compile, because by the time of the struct A definition, the compiler doesn't know what B is and it will throw an error message at you. But I can add a structure forward declaration, essentially a structure declaration, and the compiler will be happy:
Code:
struct B; // forward declaration
struct A {
B *b;
};
struct B {
A *a;
};
The "struct B;" declaration doesn't tell me everything about a structure. I don't know what the members are, so I can't access any of them. Most importantly, I don't know how big a structure of type B is, since that would require knowing the size of the elements. And C++ doesn't let you create an object, an instance of a structure, without knowing how big it is. This is what prevents me from doing
Code:
struct B; // forward declaration
struct A {
B b; // error: forward-declared structure, don't know size of B
};
struct B {
A a;
};
which would require infinite memory. Oops.
So now for the last type, variables. Consider global variables. If I say
in some file, that's a variable definition: it tells you everything you need to know about the variable. Where it is (in global scope), what the name is, what the type is. But if I put the same line in another file, this would generate a linker error. The linker would be trying to resolve all undefined symbols and combine all existing symbols into one object (the executable), and it would find that the symbol "global" was defined twice. It would be like having a main() in one file and another main() in a different file. So what you can do is to leave the definition of global in one file and in all other files that need to use the variable, put instead
This is a variable declaration, because it tells the compiler and the linker, "Somewhere else there's a definition of this variable of this type. I'm not telling you where, that's the linker's job, but for now you should mark this symbol as unresolved and not allocate any memory for it."
Now for one last note: your idea of "blueprints" is related to the issue of declaration/definition. When you say
Code:
struct POINT
{ float x, y; };
you're creating a blueprint, a description for what all POINTs will look like. This doesn't allocate any memory. But when you say
then you're instantiating the structure to create variables. A struct definition by itself just exists without taking any memory, and instances of the struct or class (which are referred to as objects) are the actual variables that you know and trust.
You can find more information about this by googling for "declaration vs definition" or something similar.
Which book are you using, just out of curiosity?