Skip to content

Instantly share code, notes, and snippets.

@meshell
Created February 22, 2016 20:40
Show Gist options
  • Save meshell/8ad4ff9c33f4bf1f0cbc to your computer and use it in GitHub Desktop.
Save meshell/8ad4ff9c33f4bf1f0cbc to your computer and use it in GitHub Desktop.
Stupid PIMPL example.
#include "pimpl.h"
#include <iostream>
#include <string>
#include <vector>
class foo::impl {
public:
void initialize() {
member1 = "Hello";
member2 = "World";
member3 = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
}
void print() {
print_member3();
std::cout << member1 << member2 << std::endl;
}
void print_member3() {
auto it = std::begin(member3);
do {
std::cout << (*it);
if(++it != std::end(member3)) {
std::cout << ", ";
}
} while (it != std::end(member3));
std::cout << "..." << std::endl;
}
private:
std::string member1;
std::string member2;
std::vector<int> member3;
};
foo::foo(): pimpl(std::make_unique<impl>()) {
pimpl->initialize();
}
foo::~foo() = default;
foo::foo(const foo& rhs) : pimpl(std::make_unique<impl>(*rhs.pimpl))
{}
foo& foo::operator=(const foo& rhs) {
*pimpl = *rhs.pimpl;
return *this;
}
foo::foo(foo&&) = default;
foo& foo::operator=(foo&&) = default;
void foo::print() {
pimpl->print();
}
#ifndef PIMPL_H
#define PIMPL_H
#include <memory>
class foo
{
public:
foo();
void print();
~foo();
foo(const foo&);
foo& operator=(const foo&);
foo(foo&&);
foo& operator=(foo&&);
private:
class impl;
std::unique_ptr<impl> pimpl;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment