C++ : can i create an empty function for then i redefine it?

Pages: 123
can i create an empty function like(or other different way):
1
2
3
4
void hello()
{
      //empty;
}

for then i redefine it?
Nope.
Did you ask this or a similar question before, or am I confusing you with somebody else?

EDIT: You did ask it before, multiple times.
http://www.cplusplus.com/forum/general/235433/
http://www.cplusplus.com/forum/general/234351/
Last edited on
you have right.. i'm so sorry.
so how works the weak on GNU for let me redefine the function?
(correct me if i'm wrong on something)
I don't understand the question. Are you saying GCC lets you redefine functions? I'm not aware of any such extension. Can you post an example?
1
2
3
#if defined __GNUC__
    #define EVENT [[gnu::weak]]
#endif 

now how we can use it on a class:
EVENT void MouseClick(); //isn't defined, but i can do it, if i need
now we can test the function if is defined:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#define IS_EVENT_DEFINED(sym) (static_cast<bool>(sym))

//.......
template <typename CBase>
class test
{
	CBase* atPointer = nullptr;
    virtual void Move(){;};
public:
    //virtual void MouseClick();
	test(CBase *clsPointer) : atPointer(clsPointer)
	{
		if (IS_EVENT_DEFINED(&CBase::MouseClick))
        {
            atPointer->MouseClick();
        }
//................ 

and the compiler don't give me any error about the function not been defined.
i don't know how the weak works.. what i know is that isn't an ANSI code :(
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.
Last edited on
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.
Last edited on
can i create an empty function
then i redefine it?

What do you actually want to achieve?

It is quite likely that your real goal is feasible to implement by a different approach.
ok see these entire code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <functional>

//we create the functions prototypes using these macro:
#if defined __GNUC__
    #define EVENT [[gnu::weak]]
#endif

//we test if the function was defined with these macro:
#define IS_EVENT_DEFINED(sym) (static_cast<bool>(sym))


template <typename CBase>
class test
{
	CBase* atPointer = nullptr;
public:
	test(CBase *clsPointer) : atPointer(clsPointer)
	{
		if (IS_EVENT_DEFINED(&CBase::MouseClick))
			atPointer->MouseClick();

		if (IS_EVENT_DEFINED(&CBase::Move))
			atPointer->Move();
	}

	void call()
	{
		if (IS_EVENT_DEFINED(&CBase::MouseClick))
			atPointer->MouseClick();

		if (IS_EVENT_DEFINED(&CBase::Move))
			atPointer->Move();
	}
};

class InstanceName : public test <InstanceName>
{
public:
    InstanceName(): test(this){;}
    ~InstanceName(){;}
public:
    EVENT void MouseClick();
    EVENT void Move();
} InstanceName;

//we can comment or uncomment these 2 definitions functions:
/*void InstanceName::MouseClick()
{
    std::cout << "Mouse click\n";
}*/

void InstanceName::Move() //i can define it outside the class
{
   std::cout << "Move\n";
}

int main()
{
    InstanceName.call();
    std::cin.get(); //press enter before exit
    return 0;
}

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?
You could use a static std::function.

1
2
3
4
5
6
7
8
9
10
11
12
class A{
public:
    static std::function<void(A &)> MouseClick;
};

int main(){
    if (rand() % 2)
        A::MouseClick = [](A&){ std::cout << "MouseClick()!\n"; };
    A a;
    if (A::MouseClick)
        A::MouseClick(a);
}
The syntax isn't quite the same, but the semantics are exactly what you're looking for. Be careful around threads, though!
helios maybe you have right.
but let me ask you 1 thing: on base class the function type can be virtual(ok it's a variable and not a function)?
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.
helios wrote:
Your previous post asked about how to emulate gnu::weak, but now you're asking about virtual functions.

Not just that:
Cambalinho wrote:
i can't use virtual functions
Cambalinho wrote:
on base class the function type can be virtual?

Why to ask about virtuals at all, if you can't use them?
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.

(The last time I thought about your problem was 3 weeks ago: http://www.cplusplus.com/forum/general/236444/#msg1058106 , after which you probably found out that MSVC doesn't really support GCC-like weak linking.)
Last edited on
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.
What concrete benefit does one (imagine to) get from not existing?

1
2
3
4
5
6
7
8
9
10
11
12
13
class Foo
{
  bool clickEnabled {false};
public:
    void MouseClick();
};

void Foo::MouseClick()
{
  if ( clickEnabled ) {
    // clickety clak
  }
}

Too naive?
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?
Then std::function should solve your problem portably. Why don't you just go with the solution I proposed above?
helios: only the static std::function can be changed on Global Scope. but if i use the static i must give it a value. that's why i don't use it.
Pages: 123