I'm unsure when should I use words 'declaration', 'definition', 'implementation'..
Could someone explain what is the proper way of using words above (what exactly each of them means)?
Thanks a lot
I'm unsure when should I use words 'declaration', 'definition', 'implementation'..
Could someone explain what is the proper way of using words above (what exactly each of them means)?
Thanks a lot
A lot of these words are used interchangably, so it's kind of hard to pin down what people mean when they use those words. Here's generally what I think they are supposed to mean:
Declaration:
Implementation:Code:int x; // Variable int f(int a); // Function
Definition:Code:int f(int a) // Function { fire_death_ray(); }
Uh....
lolCode:typedef SOMEINT int // :D
I suppose if you declare a variable as extern in a header file and then redeclare it normally in a cpp file, then that would count as a declaration vs an implementation.
Edit: I'm going to get eaten alive in this topic for this post. I can see it coming. But I'm pretty tired at the moment (24+ hours ftw!), so that's my excuse.
Implementation basically is only code that represents a concept. You can implement a Matrix class, a Graphics engine, a compression algorithm..Code:void f( ); // declaration void f( ) // definition { return; }
http://c-faq.com/sx1/index.html#declarations
http://c-faq.com/decl/decldef.html
And maybe some differences between C and C++ here:
http://david.tribble.com/text/cdiffs.htm
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
That is both a declaration and a definition (unless contained inside a class or struct or union).Code:int x; // Variable
A declaration declares something by saying "this thing exists". A declaration might also be a definition, but it doesn't have to be. (Some people might say that technically if the declaration is a definition, it isn't a declaration.) Here are some declarations that are not also definitions:A definition defines the thing. For a variable, an instance of that variable is created. For a function, the implementation of that function is given. For a class, the members of the class are defined.Code:class Foo; // forward declaration of a class extern int x; // extern variable declaration extern int y; // extern variable declaration void bar(); // function declarationCode:class Foo // class definition { public: Foo(); // class member function declaration private: static int data; // class static member variable declaration }; Foo::Foo() { } // class member function definition int Foo::data = 0; // class static member variable definition int x = 0; // variable definition int y; // variable definition void bar() // function definition { cout << x << ',' << y << '\n'; }
Last edited by Daved; 10-02-2007 at 02:41 PM.