Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 376 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the difference between a definition and a declaration?

#1
The meaning of both eludes me.
Reply

#2
**Declaration**

> Declarations tell the compiler that a
> program element or name exists. A
> declaration introduces one or more
> names into a program. Declarations can
> occur more than once in a program.
> Therefore, classes, structures,
> enumerated types, and other
> user-defined types can be declared for
> each compilation unit.



**Definition**

> Definitions specify what code or data
> the name describes. A name must be
> declared before it can be used.
Reply

#3
From the C99 standard, 6.7(5):

A declaration specifies the interpretation and attributes of a set of identifiers. A *definition* of an identifier is a declaration for that identifier that:

* for an object, causes storage to be reserved for that object;
* for a function, includes the function body;
* for an enumeration constant or typedef name, is the (only) declaration of the
identifier.

From the C++ standard, 3.1(2):

A declaration is a *definition* unless it declares a function without specifying the function's body, it contains the extern specifier or a linkage-specification and neither an initializer nor a function-body, it declares a static data member in a class declaration, it is a class name declaration, or it is a typedef declaration, a using-declaration, or a using-directive.

Then there are some examples.

So interestingly (or not, but I'm slightly surprised by it), `typedef int myint;` is a definition in C99, but only a declaration in C++.
Reply

#4
Couldnt you state in the most general terms possible, that a declaration is an identifier in which no storage is allocated and a definition actually allocates storage from a declared identifier?

One interesting thought - a template cannot allocate storage until the class or function is linked with the type information. So is the template identifier a declaration or definition? It should be a declaration since no storage is allocated, and you are simply 'prototyping' the template class or function.
Reply

#5
There are interesting edge cases in C++ (some of them in C too). Consider

T t;

That can be a definition or a declaration, depending on what type `T` is:

typedef void T();
T t; // declaration of function "t"

struct X {
T t; // declaration of function "t".
};

typedef int T;
T t; // definition of object "t".

In C++, when using templates, there is another edge case.

template <typename T>
struct X {
static int member; // declaration
};

template<typename T>
int X<T>::member; // definition

template<>
int X<bool>::member; // declaration!

The last declaration was *not* a definition. It's the declaration of an explicit specialization of the static member of `X<bool>`. It tells the compiler: "If it comes to instantiating `X<bool>::member`, then don't instantiate the definition of the member from the primary template, but use the definition found elsewhere". To make it a definition, you have to supply an initializer

template<>
int X<bool>::member = 1; // definition, belongs into a .cpp file.
Reply

#6
definition means actual function written & declaration means simple declare function
for e.g.

void myfunction(); //this is simple declaration

and

void myfunction()
{
some statement;
}

this is definition of function myfunction
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through