C++ One Definition Rule (ODR): can you define the same non-inline function more than once in the same program (across translation units)?

Difficulty: Easy

Correct Answer: C++ does not allow you to define the same functions more than once in the same program

Explanation:


Introduction / Context:
The One Definition Rule (ODR) in C++ governs how many definitions of an entity (functions, objects, types) may appear in a program. Violating the ODR often leads to linker errors or undefined behavior. This question asks whether you may define the same (non-inline) function more than once in the same program.


Given Data / Assumptions:

  • We are discussing ordinary (non-inline, non-template, non-special) functions.
  • Multiple translation units are linked together to form one program.
  • Header inclusion may replicate declarations, but not definitions, unless special rules apply.


Concept / Approach:

  • Under the ODR, each function must have exactly one definition in the entire program (inline functions and templates have special rules).
  • Multiple identical definitions of a non-inline function across translation units violate the ODR.
  • Correct practice: place the function definition in a single .cpp file and declare it in headers with extern linkage as needed.


Step-by-Step Solution:

Identify entity: non-inline function.Apply ODR: only one definition is permitted in the program.Therefore, the correct answer states that multiple definitions are not allowed.


Verification / Alternative check:

Creating two .cpp files each defining the same function foo() leads to ”multiple definition” linker errors on most toolchains.


Why Other Options Are Wrong:

  • Identical definitions / separate includes / repeated includes: All still produce multiple definitions unless the function is marked inline or is a template defined consistently, which is not the case stated.
  • None of the above: Incorrect because the ODR prohibition applies.


Common Pitfalls:

  • Placing non-inline function definitions in headers and including them in multiple translation units.
  • Confusing declarations (allowed in many places) with definitions (must be unique).


Final Answer:

C++ does not allow you to define the same functions more than once in the same program

More Questions from Object Oriented Programming Using C++

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion