在头中隐藏C ++类,而不使用未命名的命名空间 [英] Hiding a C++ class in a header without using the unnamed namespace

查看:183
本文介绍了在头中隐藏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 ++类,而不使用未命名的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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