[英]Can't use helper function with same name as member function
This code compiles (Using GCC / C++11): 这段代码会编译(使用GCC / C ++ 11):
void doStuff_HELPER(int a) { /**/ }
class SomeClass{
public:
void doStuff() {doStuff_HELPER( 10);}
};
This doesn't: 这不是:
void doStuff(int a) { /**/ }
class SomeClass{
public:
void doStuff() {doStuff( 10);}
};
It doesn't say it's ambiguous or it can't be overloaded or anything it just says: "no matching function SomeClass::doStuff(int)", "candidate: void SomeClass::doStuff()". 它并没有说它是模棱两可的,也不能重载,或者它只是说了什么:“没有匹配的函数SomeClass :: doStuff(int)”,“候选:void SomeClass :: doStuff()”。 Is this the correct behavior? 这是正确的行为吗? What does the standard say about this? 标准对此有何说法?
(Also, what is the best practice for such helper functions? Should they be put into a separate namespace maybe?) (此外,此类辅助函数的最佳实践是什么?应该将它们放在单独的命名空间中吗?)
调用它明确指定范围::doStuff(10);
You should use the Scope Resolution Operator to resolve this. 您应该使用范围解析运算符解决此问题。 Using ::doStuff(10)
instead of doStuff(10)
tells the compiler to look in the global namespace to resolve the name collision. 使用::doStuff(10)
代替doStuff(10)
告诉编译器在全局名称空间中查找以解决名称冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.