Open chen3feng opened 9 months ago
In header file:
class Foo { public: void Method(); private: class Impl; Impl* AsImpl(); std::aligned_storage<64, 8> storage_; };
In cpp file:
class Foo::Impl { public: int x; int y; string s; }; static_assert(sizeof(Foo) == sizeof(Foo::Impl)); static_assert(aligof(Foo) == aligof(Foo::Impl)); Foo::Impl* Foo::AsImpl() { return reinterrept_cast<Foo::Impl*>(&storage_); } void Foo::Method() { Impl* impl = AsImpl(); impl->...; }
class Foo { public: int GetX() const { return x; } int GetY() const { return y; } private: int x; int y; friend class PrivateOps; }
class Foo::PrivateOps { public: void Method1(Foo* foo); void Method2(Foo* foo); };
方法1
In header file:
In cpp file:
方法2
In header file:
In cpp file: