What is the difference between a definition and a declaration?

原帖: http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration

===============================================================================================================================

declaration introduces an identifier and describes its type, be it a type, object, or function. A declaration iswhat the compiler needs to accept references to that identifier. These are declarations:

extern int bar;
extern int g(int, int);
double f(int, double); // extern can be omitted for function declarations
class foo; // no extern allowed for class declarations

definition actually instantiates/implements this identifier. It's what the linker needs in order to link references to those entities. These are definitions corresponding to the above declarations:

int bar;
int g(int lhs, int rhs) {return lhs*rhs;}
double f(int i, double d) {return i+d;}
class foo {};

A definition can be used in the place of a declaration.

An identifier can be declared as often as you want. Thus, the following is legal in C and C++:

double f(int, double);
double f(int, double);
extern double f(int, double); // the same as the two above
extern double f(int, double);

However, it must be defined exactly once. If you forget to define something that's been declared and referenced somewhere, then the linker doesn't know what to link references to and complains about a missing symbols. If you define something more than once, then the linker doesn't know which of the definitions to link references to and complains about duplicated symbols.


Since the debate what is a class declaration vs. a class definition in C++ keeps coming up (in answers and comments to other questions) , I'll paste a quote from the C++ standard here.
At 3.1/2, C++03 says:

A declaration is a definition unless it [...] is a class name declaration [...].

3.1/3 then gives a few examples. Amongst them:

struct S { int a; int b; }; // defines S, S::a, and S::b
struct S; // declares S

To sum it up: The C++ standard considers struct x; to be a declaration and struct x {}; a definition. (In other words, "forward declaration" is somewhat of a misnomer, since there are no other forms of class declarations in C++.)

Thanks to litb (Johannes Schaub) who dug out the actual chapter and verse in one of his answers.

===============================================================================================================================

From the C++ standard section 3.1:

declaration introduces names into a translation unit or redeclares names introduced by previous declarations. A declaration specifies the interpretation and attributes of these names.

The next paragraph states (emphasis mine) that a declaration is a definition unless...

... it declares a function without specifying the function’s body

void sqrt(double);  // declares sqrt

... it declares a static member within a class definition

struct X
{
    int a;         // defines a
    static int b;  // declares b
};

... it declares a class name

class Y;

... it contains the extern keyword without an initializer or function body

extern const int i = 0;  // defines i
extern int j;  // declares j
extern "C"
{
    void foo();  // declares foo
}

... or is a typedef or using statement.

typedef long LONG_32;  // declares LONG_32
using namespace std;   // declares std

Now for the big reason why it's important to understand the difference between a declaration and definition: theOne Defintion Rule. From section 3.2.1 of the C++ standard:

No translation unit shall contain more than one definition of any variable, function, class type, enumeration type, or template.

===============================================================================================================================

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.

===============================================================================================================================

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 ofX<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.

===============================================================================================================================

From wiki.answers.com:

The term declaration means (in C) that you are telling the compiler about type, size and in case of function declaration, type and size of its parameters of any variable, or user defined type or function in your program. Nospace is reserved in memory for any variable in case of declaration. However compiler knows how much space to reserve in case a variable of this type is created.

for example, following are all declarations: extern int a; struct _tagExample { int a; int b; }; int myFunc (int a, int b);

Definition on the other hand means that in additions to all the things that declaration does, space is also reserved in memory. You can say "DEFINITION = DECLARATION + SPACE RESERVATION" following are examples of definition: int a; int b = 0; int myFunc (int a, int b) { return a + b; } struct _tagExample example;

see Answers.

===============================================================================================================================

Declaration "Somewhere, there exists a foo".

Definition: "...and here it is!"

===============================================================================================================================

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++.

===============================================================================================================================



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. Bytecode is an intermediate code that is generated by a compiler to be executed by a virtual machine. It is platform-independent and can be executed on any platform that has a virtual machine installed. Machine code, on the other hand, is the binary code that is directly executed by a computer's CPU. Here are some examples of instructions in bytecode and machine code: - Bytecode (CIL): ldloc.0 (load local variable 0), ldc.i4.5 (load constant value 5), add (addition) - Machine code: mov eax, [ebp-4] (move value from memory to register), mov ebx, 5 (move constant value to register), add eax, ebx (addition) 2. Autoboxing and autounboxing is not needed in C# because C# has a feature called "value types" that allows the value types to be treated like objects. This means that value types can be used in collections and passed as parameters to methods without the need for autoboxing and autounboxing. 3. Auto-implemented properties should be used when there is no additional logic required for the getter or setter methods. If additional logic is required, then a regular property with custom getter or setter methods should be used. 4. Structs and classes are similar in that they both can contain fields, methods, and properties. The main difference is that structs are value types, while classes are reference types. This means that structs are passed by value, while classes are passed by reference. 5. A regular parameter declaration should be used when the method only needs to read the value of the parameter. A ref parameter declaration should be used when the method needs to modify the value of the parameter. 6. The "new" modifier is used to hide a method or property in a derived class, while the "override" modifier is used to replace a method or property in a base class with a new implementation in a derived class. 7. A two-dimensional array should be used when the data is logically a matrix or a table with rows and columns. An array of arrays should be used when the data is logically a collection of arrays. 8. In Java, the switch statement can cause errors if there are duplicate case values or if the break statement is not used after each case. In C#, the switch statement automatically eliminates these errors by requiring a default case and not allowing fall-through cases. 9. The foreach loop is used to iterate over a collection of items, such as an array or a list. Here is an example: ``` int[] numbers = { 1, 2, 3, 4, 5 }; foreach (int number in numbers) { Console.WriteLine(number); } ``` 10. The program will print "True" and "False" because the delegate "f" contains two methods, "IsEven" and "IsSmallerThan3", which are both called when "f" is invoked with a parameter. The "+=" operator is used to add the "IsSmallerThan3" method to the delegate "f". A delegate is a type that encapsulates a method or a group of methods, and the "+=" operator is used to add a method to a delegate's invocation list.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值