Hiding a C++ class in a header without using the unnamed namespace(在不使用未命名命名空间的情况下将 C++ 类隐藏在标头中)
问题描述
我正在编写一个 C++ 头文件,我在其中定义了一个
I am writing a C++ header in which I define a
class A {
// ...
};
我想对外界隐藏(因为它可能会在此标头的未来版本中更改甚至被删除).
that I would like to hide from the outside world (because it may change or even be removed in future versions of this header).
在同一个头文件中还有一个 B 类,它有一个 A 类的对象作为成员:
There is also a class B in the same header that has an object of class A as a member:
class B {
public:
// ...
private:
A a_;
};
什么是对外界隐藏A级的正确方法?
What is a proper way of hiding class A from the outside world?
如果我将 A 的定义放在未命名的命名空间中,编译器会发出警告,因此我假设由于内部链接问题,我应该做其他事情.
If I put the definition of A in an unnamed namespace, the compiler issues a warning, so I assume that, due to issues with internal linkage, I should do something else.
推荐答案
你可以做一个内部类:
class B
{
class A { /* ... */ };
A a_;
}
这篇关于在不使用未命名命名空间的情况下将 C++ 类隐藏在标头中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在不使用未命名命名空间的情况下将 C++ 类隐藏在标头中
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
