谁能解释一下这个语法以及结果为何是这样?

Jenf_Mu 2012-12-15 02:02:14
#include <iostream>    

using namespace std;

struct strprint
{
int data;
strprint(){
cout<<"constructor"<<data<<endl;
}
void operator() (int a)
{
cout<<data<<":"<<a<<endl;
}
};

struct strprint2
{
int data;
strprint2(int _data):data(data){
cout<<"constructor"<<data<<endl;
}
void operator() (int a)
{
cout<<data<<":"<<a<<endl;
}
};

int main()
{
strprint()(8);
strprint2(1)(9);


return 0;
}


constructor-858993460
-858993460:8
constructor-858993460
-858993460:9
请按任意键继续. . .
...全文
94 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jenf_Mu 2012-12-15
  • 打赏
  • 举报
回复
strprint()(8) 这句,有人可能认为是前面的strprint()是调用默认构造函数,但在另一个例子里,它是作为operator()出现的。就是有一本书里提到 for_each(vectorA.begin(), vectorB.begin(), strprint()) 第三个参数需要一个函数指针,

1,658

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 非技术类
社区管理员
  • 非技术类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧