Link to home
Start Free TrialLog in
Avatar of Nusrat Nuriyev
Nusrat NuriyevFlag for Azerbaijan

asked on

Question about One Definition Rule

Hello dear EE-ers,

There is a quote:
In the entire program, an object or non-inline function cannot have more than one definition; if an object or function is used, it must have exactly one definition. You can declare an object or function that is never used, in which case you don't have to provide a definition. In no event can there be more than one definition.

Please, provide example about this statement, especially regarding "in no even can there be more that one definition".

Thanks.
SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Akashay K
Akashay K

Further Learning Resource
Google Code
One Definition Rule Violation
Avatar of Nusrat Nuriyev

ASKER

So, definition is more general term than implementation and in case of C programming language definition is reduced to implementation, correct?
@Akashay K
Thanks for ODR violation,  could you please provide something that I can test to violate in gcc compiler?
>>So, definition is more general term than implementation and in case of C programming language definition is reduced to
>>implementation, correct?

Indeed. I'd usually also go by the term 'implementation', but 'definition' here means the same and also covers instances and variables.
I have got it, if we say
int i = 6; //integer i implements number 6

Open in new window

, it sounds a bit weird :)

Defining instances of class?
//tu1.cpp
struct X {
    X(int);
    X(int, int);
};
X::X(int = 0) { }
class D: public X {};
D d2;


int main(){}

Open in new window

//tu2.cpp
struct X {
    X(int);
    X(int, int);
};
X::X(int = 0, int = 0) {}
class D: public X { };

Open in new window


g++ tu1.cpp tu2.cpp
compiles like a charm.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
jkr, Could you modify this links in order to make them cross reference? Still don't get this.
The problem about doing so is that it would defy the very purpose of your example. E.g.

// X.h
struct X {
    X(int);
    X(int, int);

Open in new window

// tu1.cpp
#include "X.h"
X::X(int = 0) { }
class D: public X {};
D d2;

Open in new window

#include "X.h"
X::X(int = 0) {int i = i;}
class D: public X { };

Open in new window

which correctly yields
$ g++ tu1.cpp tu2.cpp
/tmp/cc9v8yr7.o:tu2.cpp:(.text+0x0): multiple definition of `X::X(int)'
/tmp/ccsPYguC.o:tu1.cpp:(.text+0x0): first defined here
/tmp/cc9v8yr7.o:tu2.cpp:(.text+0x0): multiple definition of `X::X(int)'
/tmp/ccsPYguC.o:tu1.cpp:(.text+0x0): first defined here
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Some minor bugs have been fixed in expert's answer.