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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s