作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
多态性和运算符重载混合在一起了吗?没有指针就无法实现多态性,如 this answer 中所述。而且你不能像解释的那样用指针重载运算符 here .
所以真的没有办法做到这一点,对吧?
最佳答案
是的。您没有正确阅读答案。
这是一个简短的演示:
#include <iostream>
using namespace std;
struct X
{
int value;
virtual void operator += (int x)
{
value += x;
}
};
struct Y : X
{
virtual void operator += (int x)
{
value *= x;
}
};
void do_stuff(X& x)
{
x += 10;
cout << "Final value " << x.value << endl;
}
int main()
{
X x;
x.value = 10;
Y y;
y.value = 10;
do_stuff(x);
do_stuff(y);
return 0;
}
我并不是在暗示这是个好主意,或者它很实用。这只是可能的。
关于C++运算符重载和多态性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11258617/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!