c ++为什么不能使用指针调用overloaded()with->

 佳君旭博21 发布于 2023-02-12 09:57

只是出于好奇.如果允许使用 - >通过指针调用函数,为什么不通过 - >调用operator()?

#include 

using namespace std;

struct point {
   int x;
   int y;
   void show() {cout<< x <<" : "<< y <show();
   (*ptr)();    // ok
   ptr->();     // error
   return 0;
}

错误:

main.cpp:20:9: error: expected unqualified-id before '(' token
ptr->();

我知道ptr-> func()等价于(*ptr).func(),因此可以直接从指针调用重载运算符,而无需解除引用.函数和重载运算符之间的内部差异是什么?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有