Search code examples
c++namespacesheaderforward-declarationpimpl-idiom

Hiding a C++ class in a header without using the unnamed namespace


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).

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_;
};

What is a proper way of hiding class A from the outside world?

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.


Solution

  • You could do an inner class:

    class B
    {
      class A { /* ... */ };
      A a_;
    }