fork(1) download
  1. #include <iostream>
  2. #include <functional>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. class Base{
  8. private:
  9. int i;
  10. public:
  11. const function<int()> getValue;
  12. Base(int i, int j):getValue([=](){ return j; }){
  13. this->i = i;
  14. }
  15. };
  16.  
  17. int main() {
  18. const int N = 5;
  19. vector<Base*> bases;
  20.  
  21. for(int i=0; i<5; i++){
  22. bases.emplace_back(new Base(i, i*i));
  23. }
  24. for(Base* base: bases){
  25. cout << base->getValue() << endl;
  26. //cout << base->getValue.beta << endl;//NG//‘int GetValue::beta’ is private within this context
  27. }
  28. return 0;
  29. }
Success #stdin #stdout 0s 4552KB
stdin
Standard input is empty
stdout
0
1
4
9
16