Last active
June 14, 2023 14:47
-
-
Save piusayowale/afcb2c68a210f9657ebfd9c482d2e3fa to your computer and use it in GitHub Desktop.
c++ example of how to compose an operation
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 <string> | |
#include <memory> | |
class Base{ | |
public: | |
Base(){ | |
} | |
virtual void operation() = 0; | |
static std::string data; | |
}; | |
std::string Base::data; | |
class SetName : private Base{ | |
public: | |
void operation() override{ | |
this->data = "Pius"; | |
} | |
}; | |
class GetName : private Base{ | |
public: | |
void operation() override{ | |
std::cout << this->data << "\n"; | |
} | |
}; | |
class Checker : private Base{ | |
public: | |
void operation() override{ | |
if(this->data == "Pius"){ | |
std::cout << "Data has been set to Pius\n"; | |
}else{ | |
std::cout << "Still not set\n"; | |
} | |
} | |
}; | |
template<typename... Classes> | |
class RunAllHelper; | |
template<typename Class, typename... RemainingClasses> | |
class RunAllHelper<Class, RemainingClasses...> { | |
public: | |
void execute() { | |
Class obj; | |
obj.operation(); | |
RunAllHelper<RemainingClasses...> helper; | |
helper.execute(); | |
} | |
}; | |
template<> | |
class RunAllHelper<> { | |
public: | |
void execute() { | |
} | |
}; | |
template <typename... T> | |
class Runall{ | |
public: | |
void operator ()(){ | |
RunAllHelper<T...> helper; | |
helper.execute(); | |
} | |
}; | |
int main(){ | |
Runall<SetName, GetName, Checker> run; | |
run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment