C++ 呼叫成员C++;函数,但C函数具有相同的名称
我用的是C库C++ 呼叫成员C++;函数,但C函数具有相同的名称,c++,c,function,namespaces,member,C++,C,Function,Namespaces,Member,我用的是C库 #define read _io_read 但是我有一个C++类,继承了一个成员函数“Read”。 我试图从另一个成员函数调用成员函数,但是 编译器认为我试图在全局范围内调用C函数 我尝试了以下方法 //header namespace myNameSpace{ class B : public A{ int read(); int do_read(); } } //cpp using namespace myN
#define read _io_read
但是我有一个C++类,继承了一个成员函数“Read”。 我试图从另一个成员函数调用成员函数,但是 编译器认为我试图在全局范围内调用C函数
我尝试了以下方法//header
namespace myNameSpace{
class B : public A{
int read();
int do_read();
}
}
//cpp
using namespace myNameSpace;
int B::read(){
//other code needed
_io_read();
}
int B::do_read(){
this.read(); // here is where compiler resolves to _io_read
}
这附近有什么地方吗?我宁愿不重命名基类A的read函数,因为我无法更改非我的代码
TIA您可以使用:
int B::do_read(){
#undef read
this.read(); // here is where compiler resolves to _io_read
#define read _io_read
}
我想把答案改为:在你的模块中,你可以重新定义所有令人不快的定义,使之不那么模棱两可。例如,如果#定义的C例程来自libqux,则可以编辑qux.h来重新定义它们:
/*#define read _io_read*/
#define quux_read _io_read
唉,普通CPP没有更好的宏能力。我将其放在.CPP文件的开头,并将删除不再需要的名称空间。很抱歉将#定义为全局范围,仍在学习正确的术语:)
#define
与“全局范围内的C函数”非常不同,因此您提出问题的方式会引起误解。你应该编辑一下<代码>#定义在所有范围规则之前应用。所以,无论谁使用<代码>定义了,你所显示的方式就是编写坏C代码,这是可怕的C++代码。但是,如果您一直在包含写得不好的.h文件,您需要处理它(可能是使用#undef
),这就是为什么#define
会得到一个坏名字的原因。您需要了解#undef
。任何试图定义像read
这样的通用术语的人都应该被解雇。我没有指定是谁创建了C库是有原因的:)我已经将#undef添加到了.cpp文件的开头,并且成功了。如果我需要调用C函数,我直接将其称为_io_read。谢谢
#ifdef read
#undef read
#endif
int B::read(){
//other code needed
_io_read(); //call C function directly
}
int B::do_read(){
this.read(); //no more problem here
}