C ++包装器同名? [英] C++ wrapper with same name?

查看:153
本文介绍了C ++包装器同名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何做一个包装函数,它调用与全局命名空间中的包装函数本身完全相同的名称和参数的另一个函数?



例如我有Ah foo(int bar);并在A.cpp中的实现,并在B.h foo(int bar);和B.cpp中的foo(int bar){foo(bar)}



我想让B.cpp的foo(bar)调用A.h的foo ),不是递归本身。



我该如何做?我不想重命名foo。



更新:



在全局命名空间中,我不能改变它,所以我想使用命名空间不是一个选项?



更新: b

命名空间解决了这个问题。我不知道你可以调用全局命名空间函数与:: foo()

解决方案

B继承/实现A吗?



如果是这样,您可以使用

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

访问全局命名空间中的foo



或者如果它不继承..你可以使用B上的命名空间

 
命名空间B
{
int foo(int bar){:: foo(bar); }
};


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

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

I want that the B.cpp's foo(bar) calls A.h's foo(int bar), not recursively itself.

How can I do this? I don't want to rename foo.

Update:

A.h is in global namespace and I cannot change it, so I guess using namespaces is not an option?

Update:

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

解决方案

does B inherit/implement A at all?

If so you can use

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

to access the foo in the global namespace

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

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

这篇关于C ++包装器同名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆