Nusrat Nuriyev
asked on
Question about One Definition Rule
Hello dear EE-ers,
There is a quote:
Please, provide example about this statement, especially regarding "in no even can there be more that one definition".
Thanks.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So, definition is more general term than implementation and in case of C programming language definition is reduced to implementation, correct?
ASKER
@Akashay K
Thanks for ODR violation, could you please provide something that I can test to violate in gcc compiler?
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.
>>implementation, correct?
Indeed. I'd usually also go by the term 'implementation', but 'definition' here means the same and also covers instances and variables.
ASKER
I have got it, if we say
Defining instances of class?
int i = 6; //integer i implements number 6
, it sounds a bit weird :)Defining instances of class?
ASKER
//tu1.cpp
g++ tu1.cpp tu2.cpp
compiles like a charm.
struct X {
X(int);
X(int, int);
};
X::X(int = 0) { }
class D: public X {};
D d2;
int main(){}
//tu2.cppstruct X {
X(int);
X(int, int);
};
X::X(int = 0, int = 0) {}
class D: public X { };
g++ tu1.cpp tu2.cpp
compiles like a charm.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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);
// tu1.cpp
#include "X.h"
X::X(int = 0) { }
class D: public X {};
D d2;
#include "X.h"
X::X(int = 0) {int i = i;}
class D: public X { };
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Some minor bugs have been fixed in expert's answer.
Google Code
One Definition Rule Violation