That's ... an amazingly annoying interpretation of what you are seeing. Strong vs weak symbols are features of the ELF file format, not the C family of languages. In fact, it's not even your compiler doing this but rather your linker. That's why this is done with the pre-compiler directive #if defined . This isn't an "run-time override" of the function, it's a find and replace before the code is compiled kind of thing.
Anyway, after reading the stuff that helios posted, this isn't what you want. I'd say that a function pointer with "std::function" is the closest you're going to get, although I'll admit that I don't quite know what you're going for. You could run amok with namespaces if you truly feel the need to, but once the code is compiled the jump tables are set and the single definition rule applies.
gnu::weak works by telling the linker to relax definition requirements for the specified function.
As far as I know, it still doesn't let you redefine the function. It just lets you not define it.
I'm not sure what it is you're asking. Yes, it could be possible for standard C++ to let you redefine functions. There are no theoretical limitations that make such a feature impossible.
But I assumed your original question was about whether C++ actually lets you do that, so I replied with a "no", because it doesn't.
Hypothetically, C++ could let you redefine functions, but it doesn't.
you can use a template or function pointers to emulate this behavior, though.
what you do there is a function pointer named foo can be changed to point to implementation 1 (empty?) or implementation 2 (does whatever, maybe writes hello world) etc. This is C type programming that works in C++ but is seldom used anymore.
You can also do it with some macros, and other C idea.
So the answer is "you can implement this behavior" but the language does not really support it 'directly' in the way you might like.
another way would be a library like a dll. Simply have multiple dlls and you can load and invoke the version of the function you want to use today. This is external to the program, so you can even modify the function's task and the program will call the new version from the updated library; this is how many packages patch in new versions for bug-fixes etc.
Can you explain better what the GOAL is? Its difficult to see what you want to do.
//we create the functions prototypes using these macro:
#if defined __GNUC__
#define EVENT [[gnu::weak]]
//we test if the function was defined with these macro:
#define IS_EVENT_DEFINED(sym) (static_cast<bool>(sym))
template <typename CBase>
CBase* atPointer = nullptr;
test(CBase *clsPointer) : atPointer(clsPointer)
class InstanceName : public test <InstanceName>
EVENT void MouseClick();
EVENT void Move();
//we can comment or uncomment these 2 definitions functions:
std::cout << "Mouse click\n";
void InstanceName::Move() //i can define it outside the class
std::cout << "Move\n";
std::cin.get(); //press enter before exit
these is what i need. but i have 2 problems:
1 - i can't use virtual functions... that's why i use the pointer between the base and child class;
2 - the 'weak' isn't ANSI, so don't works with others C++ compilers.
how can i simulate the 'weak' for my code be compatible with others C++ compilers?
Your previous post asked about how to emulate gnu::weak, but now you're asking about virtual functions. Are you sure you have clear in your head the difference between the two?
* Weak linkage lets you successfully link an executable that leaves some functions undefined, and lets you check whether those functions are defined. However, a function name can only be bound to a single function throughout the program.
* Virtual functions let you use dynamic binding. That is, the function that is called by object->foo() depends on the run time type of object. However, all functions must always be defined.
This is almost certainly an XY problem (see: http://xyproblem.info/ ). I've ignored that issue previously, but since you're still working on the same thing a whole month later, it's probably time to give us some more information.
What goal do you hope to reach by re-defining a function?
It's unlikely that you're going to get any more help until you directly answer that.
It's clear this is some kind of event handling system, and OP wants event handlers to sometimes be undefined, and possibly (judging by 234351) to be defined on a per-object basis. Multiple posters, myself included, have come to the conclusion that either std::function or function pointers are the only facilities with the sufficient flexibility to implement the semantics OP wants. For some reason though, OP seems to be disregarding this advice and continues to waste time playing around with weak linking and other hacks, not to mention repeatedly asking about C++ features that don't exist (function redefinition).
It would be interesting to know why this is, as well.
i can't use static functions, in these case, because i realy must define them.
imagine i have a list of prototype functions on a macro(like the event list). i must add them on class. but some don't need to be defined, is what i mean.
that's why the 'weak' resolves the problem.
i know the 'weak' it's a linker option, but how it works?