C ++ shell with the same name?

How can I make a wrapper function that calls another function with exactly the same name and parameters as the wrapper itself in the global namespace?

For example, I have Ah foo (int bar); and in A.cpp - its implementation, and in Bh foo (int bar); and in B.cpp foo (int bar) {foo (bar)}

I want B.cpp foo (bar) to call Ah foo (int bar), and not recursively.

How can i do this? I do not want to rename foo.

Update:

Ah is in the global namespace and I can't change it, so I assume that using namespaces is not an option?

Update:

Namespaces solve the problem. I did not know that you could call the global namespace function with :: foo ()

+3
source share
5 answers

Does B inherit / implement A?

If you can use

int B :: foo (int bar) 
{ 
   :: foo (bar); 
}

to access foo in the global namespace

or if it does not inherit .. you can use the namespace only on B

namespace B
{
int foo (int bar) {:: foo (bar); }
};
+3
source

use namespace

namespace A
{
int foo(int bar);
};
namespace B
{
int foo(int bar) { A::foo(bar); }
};

You can also write using the namespace A; in your code, but it is highly recommended that you never write using the namespace in the header.

+4
source

, . foo? . , , .

+1

You cannot do this without using namespaces, changing the name of one of the functions, changing the signature, or creating one of them. The problem is that you cannot have 2 functions with the same changed name. Therefore, there must be something that makes them different.

+1
source

As mentioned above, the namespace is one solution. However, assuming you have such a level of flexibility, why don't you encapsulate this function in classes / structures?

0
source

Source: https://habr.com/ru/post/1703492/


All Articles