Skip to content

Instantly share code, notes, and snippets.

@plusangel
Last active June 5, 2020 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plusangel/b573f97e387aba413b3d0c8ea40caf3e to your computer and use it in GitHub Desktop.
Save plusangel/b573f97e387aba413b3d0c8ea40caf3e to your computer and use it in GitHub Desktop.
Monostate idiom example in C++
cmake_minimum_required(VERSION 3.16)
project(monostate_idiom)
set(CMAKE_CXX_STANDARD 17)
add_library(monostate monostate.cpp)
add_executable(monostate_idiom main.cpp)
target_link_libraries(monostate_idiom monostate)
#include <iostream>
#include "monostate.h"
int main() {
Monostate monostate{};
std::cout << "and the answer is... " << monostate.GetTheAnswer() << std::endl;
return 0;
}
//
// Created by angelos on 05/06/2020.
//
#include "monostate.h"
static int answer = 42;
int Monostate::GetTheAnswer() const {
return answer;
}
//
// Created by angelos on 05/06/2020.
//
#ifndef MONOSTATE_IDIOM_MONOSTATE_H
#define MONOSTATE_IDIOM_MONOSTATE_H
class Monostate {
public:
int GetTheAnswer() const;
};
#endif//MONOSTATE_IDIOM_MONOSTATE_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment