c++ - How does linker deal with virtual functions defined in multiple headers? -



c++ - How does linker deal with virtual functions defined in multiple headers? -

suppose have

base.h class base of operations { virtual void foo() {...} }; derived1.h class derived1 : public base of operations { virtual void foo() {...} }; derived2.h class derived2 : public base of operations { virtual void foo() {...} };

header derived1.h included in multiple source files , derived1 class used through base interface. since foo virtual , used polymorphic can not inlined. compiled in multiple obj files. how linker resolve situation?

member functions defined within class definition implicitly inline(c++03 7.1.2.3). whether function body gets inlined @ point of calling immaterial. inline allows have multiple definitions of function long definitions same(which disallowed 1 definition rule)(c++03 7.1.2.2). standard mandates linker should able link (one or)many of these definitions.(c++03 7.1.2.4).

how linker this?

the standard provisions by:

it mandates function definition should nowadays in each translation unit. linker has link definition found in translation unit. it mandates definitions of function should same, removes ambiguity of linking particular definition, if different definitions exist.

c++03 7.1.2 function specifiers: para 2:

a function declaration (8.3.5, 9.3, 11.4) inline specifier declares inline function. inline specifier indicates implementation inline substitution of function body @ point of phone call preferred usual function phone call mechanism. implementation not required perform inline substitution @ point of call; however, if inline substitution omitted, other rules inline functions defined 7.1.2 shall still respected.

para 3:

a function defined within class definition inline function. inline specifier shall not appear on block scope function declaration

para 4:

an inline function shall defined in every translation unit in used , shall have same definition in every case (3.2).

c++ linker polymorphism

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -