C++ – One Definition Rule

One Definition Rule or ODR is an important rule in C++ which loosely states the following.

In any translation unit, you cannot provide more than one definition for a variable (of basic type and complex type) or a function or a class (class and struct) or an object. You are allowed to have multiple declarations within the same translation unit but multiple definitions are not allowed. It is the compiler that usually catches multiple definitions within a translation unit and throws an error.

In any program consisting of multiple translation units, you cannot provide more than one definition for a non- inline function or an object. But you can provide multiple definitions for a variable, a class or an inline function as long as the definitions in all the translation units are exactly the same. You are allowed to have multiple declarations across translation units. Multiple definition error across translation units is usually caught by the linker.

 

 

 

One thought on “C++ – One Definition Rule

Add yours

Leave a comment

Create a website or blog at WordPress.com

Up ↑