Who can tell me the difference between definition and declaration?
My book said that definition is also declaration, then what's the need for C++ to distinguish definition and declaration?
Who can tell me the difference between definition and declaration?
My book said that definition is also declaration, then what's the need for C++ to distinguish definition and declaration?
Because not all declarations are definitionsOriginally Posted by Allen
The difference is important when considering the one definition rule, e.g., you cannot define a class more than once in the same translation unit, and you cannot define the same variable or function more than once in the entire program. However, you can declare a class, variable (using extern) and function many times.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
There are times when you want to just declare something without defining it. When you define something, like a variable or function, it takes up a home, i.e., a memory location with an address. So obviously, you don't want to define something twice, or else it will have two memory locations with two addresses. That would be like having two houses on the same street with the same address, and that would create ambiguity for the pizza delivery guy!
In many cases, an object or function is already defined elsewhere, and out of sight of the compiler, so then only a declaration needs to be made so the compiler knows what it is without defining it again and creating ambiguity. An example of this would be the library functions that you use, like the iostream class for cout, etc. These functions are already defined and have a "home" and will be linked in to your program later. But for now, the compiler needs to know about it, so it just needs to be declared, not defined. The #include<iostream> header contains the declaration for these functions so the compiler can have record of them and can check to make sure the functions are being used correctly.
Another example of when you need to declare but not define is with the functions that you write yourself. If you had two functions, say function A and function B, and function A calls function B and likewise function B calls function A, then how could you ever define these two functions? If you try to define function A first, then the compiler will complain because function A calls function B which the compiler knows nothing about. And if you try to define function B first, which calls A, then the compiler won't know what function A is and will complain. So you are forced to declare one of the functions first, say B, then you can define function A. Once function A is defined (and thus declared) you can define function B which calls A.
Things can get hairy when you have like 10 functions in your program, and function 7 calls function 2 and 4 calls 6 and 9 calls both 1 and 2. Then when you define your functions, you have to make sure you define them all in the right order, if that's even possible, so that any one function that is defined is defined after any functions that it calls within itself. My practice is to always declare my functions first, before main() using prototypes, and then define my functions in any order later, like after main().
With a variable, like int x, the definition is also the declaration, and you can not separate the two. With functions, you can make a declaration separate from the definition, but not so with simple variable types. Yet, there are times when you want to declare a variable without defining it again. Let's say you had two source files for your program, file A and file B, which would be combined by the linker after the compilation process (let's say your friend is writing file A and you are doing file B - it's a large program). File A is already written and in it int x is defined (and declared). You are now writing file B and you want to manipulate variable x in file A. Well, since x is already defined in file A and has a memory address already assigned to it, you only need to declare it in file B so the compiler knows about it and accepts it. And since you can't separate the definition from the declaration, you need to add a keyword to the variable name. This keyword is extern, and is used like this: extern int x;. This tells the compiler to look at int x only as a declaration and not as a definition, because it is already defined elsewhere (externally). This way, if you wanted to, you could declare a variable before main(), use it in main(), then actually define it after main(), just like a function.
So in summary, there are many times when something has already been brought into existence (defined) but needs to be declared, or has yet to be brought into existence but is being used anyway, thus it needs to be declared so the compiler knows about it and knows how to code it (i.e., translate it).
Nice explanation!
Well, they(i.e cin, cout..etc) aren't functions but objects.An example of this would be the library functions that you use, like the iostream class for cout, etc. These functions are already defined and have a "home" and will be linked in to your program later. But for now, the compiler needs to know about it, so it just needs to be declared, not defined. The <iostream> header contains the declaration for these functions so the compiler can have record of them and can check to make sure the functions are being used correctly.
thanks