Last active
June 5, 2020 18:54
-
-
Save plusangel/b573f97e387aba413b3d0c8ea40caf3e to your computer and use it in GitHub Desktop.
Monostate idiom example in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "monostate.h" | |
int main() { | |
Monostate monostate{}; | |
std::cout << "and the answer is... " << monostate.GetTheAnswer() << std::endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by angelos on 05/06/2020. | |
// | |
#include "monostate.h" | |
static int answer = 42; | |
int Monostate::GetTheAnswer() const { | |
return answer; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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