Skip to content

Instantly share code, notes, and snippets.

@drodil
Last active September 28, 2018 10:20
Show Gist options
  • Save drodil/a7357e446764022c6abd5e473b4d0252 to your computer and use it in GitHub Desktop.
Save drodil/a7357e446764022c6abd5e473b4d0252 to your computer and use it in GitHub Desktop.
#include <iostream>
class BaseOutputter {
public:
static void set_prefix_message(const std::string& msg) {
prefix_message = msg;
}
protected:
BaseOutputter() = default;
static std::string prefix_message;
};
std::string BaseOutputter::prefix_message = "Value is ";
template<class T>
class Outputter : public BaseOutputter {
public:
Outputter(const T& val) {
value = val;
}
void output() {
std::cout << prefix_message << value << std::endl;
}
private:
T value;
};
int main()
{
Outputter<int> intOutputter(1);
intOutputter.output();
Outputter<float> floatOutputter(1.2f);
floatOutputter.output();
BaseOutputter::set_prefix_message("Value should be ");
Outputter<std::string> strOutputter("this");
strOutputter.output();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment