Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- C++11 Quiz ( Type Traits )
- =======================
- Let's say I have the following piece of code.
- #include <iostream>
- struct Mom
- {
- virtual void play_strategy() const
- {
- std::cout << "Watching television" << std::endl;
- }
- virtual ~Mom() { }
- };
- struct FemaleChild: Mom
- {
- virtual void play_strategy() const override
- {
- std::cout << "Playing with dolls" << std::endl;
- }
- };
- struct MaleChild: Mom
- {
- virtual void play_strategy() const override
- {
- std::cout << "Kicking football around" << std::endl;
- }
- };
- struct UnrelatedWitchMom
- {
- void play_strategy() const
- {
- std::cout << "Killing innocent children" << std::endl;
- }
- };
- int main()
- {
- Mom my_mom {};
- Mom *you = new FemaleChild{}, *me = new MaleChild{};
- play_type( &my_mom );
- play_type( you );
- play_type( me );
- UnrelatedWitchMom mom_ah {};
- play_type( &mom_ah );
- delete you;
- delete me;
- return 0;
- }
- In C++, I know that I can make a template function that accepts any class object, like:
- template <class T> void iAcceptAnything(T x)
- {
- ....// Do something
- }
- Now, this works nice, but I would like to make a more restrict template class, something that ACCEPTs as T any type that INHERITs from class 'Mom'. What I would need is something like:
- template <class T:mom> void play_type(T *x)
- {
- x->play_strategy(); //or by reference, x.play_strategy();
- }
- Is this possible at all? If yes, how?
- Note that function 'play_type' has been intentionally left out, that's your task!
- Have fun!!!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement