Why is there one definition rule and not one declaration rule?

I read the materials below:

https://www.wikiwand.com/en/One_Definition_Rule

http://en.cppreference.com/w/cpp/language/definition

What is the difference between a definition and a declaration?

But still, I can’t understand why this is one rule of definition, and not the rule of one declaration?

I argue that a declaration is a subset of a definition, so one definition rule is enough.

+6
source share
5 answers

A definition is a subset of a declaration, not the other way around. Each definition is a declaration, and there are declarations that are not definitions.

int i = 3;      // definition and declaration
extern int i;   // ok: (re)declaration
int i = 4;      // error: redefinition

extern int j;   // declaration
extern int j;   // ok: (re)declaration
int j = 5;      // ok: (re)declaration and definition
int j = 6;      // error: redefinition
+4
source

, , , . .

( ) , , A.cpp B.cpp, <string>.

Header Enabled Twice

A.cpp B.cpp . <string>, std::string.

( ), , node :

// Does not compile
struct tree {
    struct node *root;
};
struct node {
    struct node *left;
    struct node *right;
    struct tree *owner;
};

, node struct node *tree . struct node struct tree , tree struct tree *owner . C ++ struct s.

+7

.h , , , , .

+6

,

// header toto.h
int f(void);

, ,

#include "toto.h"

int f(void) {
   return 0;
}

, : .c .cpp.

, .

+2

, ++ ; , :

int X();
void X(); // error

.

, ; X() ; , . .


. - , .

. X() , , , X().

, ? , , .

, ++ . /- ++ , , / , , undefined.

+1

All Articles