在类的CPP文件中求解名称扣
Solving name-clash in cpp file of a class
我想在foo的" bar"方法中从" somelib"中调用未指示的函数" bar"。
// .h
class Foo
{
int bar();
};
// .cpp
#include "Foo.h"
#include <somelib> // contains unscooped function bar()
int Foo::bar()
{
return bar(); // unwanted recursive function
}
解决方法的一种方法是使用辅助功能,例如" bar_helper"
// .cpp
#include "Foo.h"
#include <somelib> // contains unscooped function bar()
// unnamed namespace
namespace
{
int bar_helper()
{
return bar(a);
}
}
int Foo::bar()
{
return bar_helper();
}
- 可以变得更漂亮吗?
- 有更好的解决方案吗?
如果非会员bar
函数在全局范围中,则可以使用范围操作员::
:
int Foo::bar()
{
return ::bar();
}
在范围上明确说明全局一个:
return ::bar();
^^
相关文章:
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 命名空间中具有.h和.cpp文件的类
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 有充分的理由在h文件中使用include保护而不是cpp文件吗
- 如何在cpp文件之间切换窗口?在Qt中
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 库标题在标题中不可见,但在 cmake build 下.cpp文件中完全可见.为什么?
- C++ 链接到单独的.cpp文件说"multiple definitions"
- 在 cpp 文件中隐藏采用模板参数引用的方法
- 如何使用 samtools C API 构建一个简单的主.cpp文件
- 包含在.cpp文件中包含在 .h 文件时包含
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 如何使用线程类编译 cpp 文件?
- 使用 Powershell 命令将 cpp 文件的文件夹编译为 GNU 的 g++
- 我应该将外部标头放在 .h 文件还是.cpp文件中?
- 从另一个 cpp 文件更改结构内、映射键内的变量