• 一个c++函数对象不明细节
  • java
    1.00元

#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;


//函数对象做参数 做返回值
class myprint04{
public:
   myprint04() :count(0){}
   void operator()(int v){
       count++;
       cout << v << " ";
   }
   int count;
};
void test04(){

   vector<int> v;
   for (int i = 0; i < 10;i++){
       v.push_back(i);
   }

   myprint04 myp04;
   myprint04 myp05 = for_each(v.begin(), v.end(), myp04); //函数对象做参数
   cout << "count:" << myp04.count << endl; cout << endl;
   cout << "count:" << myp05.count << endl; cout << endl;
}

int main(){
   test04();
   return 0;
}

0 1 2 3 4 5 6 7 8 9 count:0

count:10

   我不理解的是myp04.count为什么是0.既然是0,我断点调试也发现对象成员

变量的值的确没变。可是不知道为什么,谁能说一下原理吗


  • 就几件    2020-05-15 10:12:39
  • 阅读 668    收藏 0    回答 1
  • 邀请
  • 收藏
  • 分享
发送
登录 后发表评论
  • 51testing软件测试圈微信