#include<iostream>
#include <functional>
#include <string>
using namespace std;
class MyClass{
public:
static const int SCREAM = 0,WHISPER = 1,SLEEP = 2;
void emote();
private:
std::function<bool(MyClass*,string) > react[3]= {&MyClass::scream,&MyClass::whisper,&MyClass::sleep};
bool scream(string s);
bool whisper(string s);
bool sleep(string s);
};
bool MyClass::scream(string s){
cout << s << endl;
return true;
}
bool MyClass::whisper(string s){
cout << s << endl;
return false;
}
bool MyClass::sleep(string s){
cout << s << endl;
return true;
}
void MyClass::emote(){
bool a,b,c;
a =react[SCREAM](this,"eeek");
b = react[WHISPER](this,"shhh");
c = react[SLEEP](this, "zzzz");
cout<< a << " " << b << " "<< c<<endl;
}
int main()
{
MyClass c;
c.emote();
}
#include<iostream>
#include <functional>
#include <string>
using namespace std;
class MyClass{
public:
static const int SCREAM = 0,WHISPER = 1,SLEEP = 2;
void emote();
private:
std::function<bool(MyClass*,string) > react[3]= {&MyClass::scream,&MyClass::whisper,&MyClass::sleep};
bool scream(string s);
bool whisper(string s);
bool sleep(string s);
};
bool MyClass::scream(string s){
cout << s << endl;
return true;
}
bool MyClass::whisper(string s){
cout << s << endl;
return false;
}
bool MyClass::sleep(string s){
cout << s << endl;
return true;
}
void MyClass::emote(){
bool a,b,c;
a =react[SCREAM](this,"eeek");
b = react[WHISPER](this,"shhh");
c = react[SLEEP](this, "zzzz");
cout<< a << " " << b << " "<< c<<endl;
}
int main()
{
MyClass c;
c.emote();
}
To copy to clipboard, switch view to plain text mode
Bookmarks