Thread: definition/declaration/...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    definition/declaration/...

    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

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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:

    Code:
    int x; // Variable
    int f(int a); // Function
    Implementation:

    Code:
    int f(int a) // Function
    {
        fire_death_ray();
    }
    Definition:

    Uh....

    Code:
    typedef SOMEINT int // :D
    lol

    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.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    void f( ); // declaration
    
    void f( ) // definition
    {
        return;
    }
    Implementation basically is only code that represents a concept. You can implement a Matrix class, a Graphics engine, a compression algorithm..

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    int x; // Variable
    That is both a declaration and a definition (unless contained inside a class or struct or union).

    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:
    Code:
    class Foo; // forward declaration of a class
    
    extern int x; // extern variable declaration
    extern int y; // extern variable declaration
    
    void bar(); // function declaration
    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 // 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.

Popular pages Recent additions subscribe to a feed