Partilhar via


extern "C" and overloading

C++ will mangle every function assuming its potential to be overloaded. so how would you make sure your overloaded functions (where all except one of the overloaded functions act as wrappers to one core function ) work with C?

Example:

Assume foo(char *) calls foo (int i) .

class Object
{

public:
void foo(int i); // primary function
void foo(char *); // secondary function
};

int _tmain(int argc, _TCHAR* argv[])
{
Object obj;
obj.foo(1);
obj.foo("string");
return 0;
}

So when this snippet is compiled, we get the following error.
error LNK2019: unresolved external symbol "public: void __thiscall Object::foo(char *)" (<?foo@Object@@QAEXPAD@Z>) referenced in function _main
error LNK2019: unresolved external symbol "public: void __thiscall Object::foo(int)" (<?foo@Object@@QAEXH@Z>) referenced in function _main

Note that functions are converted (mangled) with other characters (Ex: <?foo@Object@@QAEXPAD@Z>) to guarantee uniqueness for C++ table handling.

But if foo(char *) was a function which would call foo(int i) we could have made this in compiler-agnostic library by declaring first version as extern "C" and defining others within C++ as overloaded functions.

For example:

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

void foo(int i);

#ifdef __cplusplus
} /* extern "C" */

inline foo (char * str)
{
return foo(atoi(str));
}
#endif /* __cplusplus */

void foo (int i)
{
//definition of function
}

Thus, if you are using C compiler, you will have foo (int i ) at your disposal, and both foo (int i) and foo (char * str) otherwise. Hence your code is more portable.  Version of this problem is also discussed in Imperfect C++ by Matthew Wilson