2

I had one class with test method with body;

class Object {
public:
    Object(){

    }
    virtual ~Object(){

    }

    void test(){
    }
};

I include this object.h in 2 cpp files. Why is there no multiple definition error for functions Object::test that is available (after include) in both cpp files?

I understand that a function with complete body is a definition and not declaration, so I expect there should be multiple definition error.

is there official article talk about it?

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
jiafu
  • 6,338
  • 12
  • 49
  • 73

1 Answers1

3

According to the C++ Standard (7.1.2 Function specifiers)

3 A function defined within a class definition is an inline function.

4 An inline function shall be defined in every translation unit in which it is odr-used and shall have exactly the same definition in every case (3.2).

And

A function whose name appears as a potentially-evaluated expression is odr-used if it is the unique lookup result or the selected member of a set of overloaded functions (3.4, 13.3, 13.4), unless it is a pure virtual function and its name is not explicitly qualified.

Where ODR is an abbrevation of the One Definition Rule. Simply speaking a function is odr-used when it takes part in expressions that are evaluated.

Community
  • 1
  • 1
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335