Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // PIMPL
- #include <iostream>
- #include <string>
- template <typename T>
- class Pimpl {
- protected:
- T *const impl;
- public:
- Pimpl();
- ~Pimpl();
- };
- template <typename T>
- Pimpl<T>::Pimpl(): impl(new T()) {}
- template <typename T>
- Pimpl<T>::~Pimpl() {
- delete impl;
- }
- class Aimpl;
- class A: public Pimpl<Aimpl> {
- public:
- void inc();
- void dec();
- friend std::ostream& operator<<(std::ostream& os, const A& a);
- };
- class Aimpl {
- public:
- int i = 0;
- };
- void A::inc() {
- ++impl->i;
- }
- void A::dec() {
- --impl->i;
- }
- std::ostream& operator<<(std::ostream& os, const A& a) {
- os << a.impl->i << "\n";
- return os;
- }
- int main()
- {
- A a;
- a.inc();
- std::cout << a;
- a.dec();
- std::cout << a;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement