How to instruct the compiler to generate an alias for a virtual function?
Question
See this question for the background.
Basically, I have the following definition of a class
class MyClass {
virtual int foo4(double, int);
};
Is there a way to instruct the compiler to generate two symbols that would resolve to foo4
? That is, I want that if an executable asks for the dynamic linker to resolve _ZN7MyClass4foo4Edi
(symbol for MyClass::foo4(double, int)
) and some other symbol (let's say _ZN7MyClass9reserved1Ev
, a symbol for MyClass::reserved1()
), the dynamic linker would resolve both to &MyClass::foo4(double, int)
. I`m using fairly modern GCC on Linux.
Solution
In C++ it looks like this:
class MyClass {
int foo5(double, int) __attribute__((alias("_ZN7MyClass4foo4Edi")));
virtual int foo4(double, int);
};
int MyClass::foo4(double d, int i)
{
}
OTHER TIPS
In gcc, use the "alias" attribute.
int reserved1() __attribute__((alias ("_ZN7MyClass4foo4Edi")));
... but I believe this will only work in the same object file as (a) definition of the symbol, so I'm not sure it will suit your uses cases: see here. Specifically, it will only be an alias for one version of the virtual call, and won't be inherited by subclasses; additionally, you cannot use it to alias a weak symbol.