- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
考虑:
// member data omitted for brevity
// assume that "setAngle" needs to be implemented separately
// in Label and Image, and that Button does need to inherit
// Label, rather than, say, contain one (etc)
struct Widget {
Widget& move(Point newPos) { pos = newPos; return *this; }
};
struct Label : Widget {
Label& setText(string const& newText) { text = newText; return *this; }
Label& setAngle(double newAngle) { angle = newAngle; return *this; }
};
struct Button : Label {
Button& setAngle(double newAngle) {
backgroundImage.setAngle(newAngle);
Label::setAngle(newAngle);
return *this;
}
};
int main() {
Button btn;
// oops: Widget::setText doesn't exist
btn.move(Point(0,0)).setText("Hey");
// oops: calling Label::setAngle rather than Button::setAngle
btn.setText("Boo").setAngle(.5);
}
有什么技巧可以解决这些问题?
示例:使用模板魔法让 Button::move 返回 Button& 什么的。
edit很明显,第二个问题是通过将 setAngle 设为虚拟来解决的。
但第一个问题仍未以合理的方式解决!
edit:嗯,我想在 C++ 中不可能做到正确。无论如何感谢您的努力。
最佳答案
您可以扩展 CRTP来处理这个。 monjardin 的解决方案朝着正确的方向发展。您现在只需要一个默认的 Label
实现来将其用作叶类。
#include <iostream>
template <typename Q, typename T>
struct Default {
typedef Q type;
};
template <typename T>
struct Default<void, T> {
typedef T type;
};
template <typename T>
void show(char const* action) {
std::cout << typeid(T).name() << ": " << action << std::endl;
}
template <typename T>
struct Widget {
typedef typename Default<T, Widget<void> >::type type;
type& move() {
show<type>("move");
return static_cast<type&>(*this);
}
};
template <typename T = void>
struct Label : Widget<Label<T> > {
typedef typename Default<T, Widget<Label<T> > >::type type;
type& set_text() {
show<type>("set_text");
return static_cast<type&>(*this);
}
};
template <typename T = void>
struct Button : Label<Button<T> > {
typedef typename Default<T, Label<Button<T> > >::type type;
type& push() {
show<type>("push");
return static_cast<type&>(*this);
}
};
int main() {
Label<> lbl;
Button<> btt;
lbl.move().set_text();
btt.move().set_text().push();
}
也就是说,考虑一下这样的努力是否值得增加少量的语法奖励。考虑替代解决方案。
关于c++ - 方法链 + 继承不能很好地结合在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/551263/
下类时合上笔记本电脑。当我回到家并打开它时,它已重新启动。现在,当我打开 Titanium Developer 时,它会立即崩溃。 所以我现在也打不开。关于如何调试或找出问题所在的任何想法? 甚至尝试
我们想共享运行时项目二进制文件。所以每个团队成员都可以使用当前的工作版本。 在 SVN 中存储运行时二进制文件是否可以接受/很好? 最佳答案 不,不要将二进制文件存储在其源代码旁边(除非您有充分的理由
我在 PHP 中使用循环来显示 CMS 管理部分中的用户数据。每行(用户)都包含一些我想要连接到命令的图标(例如:编辑、删除等)。表的最后一行有空的输入字段,带有单个图标(命令),以允许添加新用户。这
如果这是一个新手问题,请原谅我,我昨天开始学习 Django,并且我尽量不要养成坏习惯,即我试图从一开始就以“django 方式”做事。 我有一个 View 接收二进制数据作为 http post 字
使用 swift 2.1 我正在寻找一种将非可选类型数组分配给可选类型数组的好方法,其中类型相同。这是我尝试过的一些方法: var foos: [Int?] = [] let bars: [Int]
James Gosling,加拿大计算机科学家,完成了 Java 的原始设计,并实现了 Java 最初版本的编译器和虚拟机,也是公认的 “Java 之父”。 Evrone 是一家企业软件开发公司,
我在两个项目上使用 Twitter Bootstrap,一个是静态 HTML 网站,另一个是 Rails 应用程序。当我在桌面浏览器上测试网站时,调整大小有效。但是当我在手机 [Nokia E72]
我可以使用全日历。但我有一个侧边栏,用户可以折叠它,然后内容框会变大,但是当用户这样做时,日历就不那么好了。 所以我正在考虑窗口调整大小功能,但这仅在浏览器窗口更改时才有效,那么当容器变大或变小时如何
我正在尝试使用 C# 和 LINQ 在数据库中查询每日活跃用户。我有一个运行良好的 SQL 查询。它在 u.UserId).Distinct().Count() } 测试后,当我必须选择 (
我目前正在开发部署目标为 7.1 的 iOS 应用程序。我的大部分测试都是在 iOS8 环境中执行的,没有任何问题。我连接了一个 iOS7 (5s) 测试设备,发现通过手机显示的 View 是 3.5
我有这样的代码: Floating left. Floating right. BlahBlah Container 允许我将页脚推到页面底部,但如果我想让左右栏跨越接触页脚的高度
从 PHP4 和 Cake 1.3 开始,我一直在使用 debug($data);在 CakePHP 中调试诸如模型输出之类的东西。 但是,自从升级到 PHP5.4 后,我注意到 debug($dat
我在Canvas上画我的游戏,一切都是上帝,但我把它改成了JPanel,但现在它不能正常工作,这是代码,你可以复制它们,你就会看到问题出在哪里(我有一个菜单,单击按钮后它应该创建新线程,我想在那里画画
我尝试用 scrapy 抓取一页。我用 FireXpath(一个 firefox 插件)找到了 xpath,它看起来不错。但是对于 Scrapy,我没有得到任何结果。 我的 python 程序如下所示
我想在页面加载时加载 fancybox。它工作正常,但我对它的高度有疑问。假设页面的高度为 3000px,而 fancybox 的高度为 1500px。如果你想看到页面的最低部分,都应该向下滚动。不幸
IE 大小调整问题!?代码非常简单:我有一个 div,我想要一个占 DIV 100% 的文本框。它必须显示 div 的红线(如果我使用 height:100%, width:100% 它会侵 ecli
我有一组看起来像这样的代码: if(self.property == A) { [self.infoManager getThingA:^(NSString *thing, NSError *
我认为 git clone 使用 STDERR。 我现在想将它重定向到 STDOUT 以使用此 hack . 我遇到了一些问题(另外,我使用很棒的 stderred 库自动将 STDERR 着色为红色
我的问题是我的表单正在提交(到节点/express api),如果我console.log req.body(@ api),结果是一个空对象。但是,如果我在客户端上 console.log ,则序列化
我对 addon-sdk 还很陌生,并且遇到了一个对我来说无法解释的问题。到目前为止,我一直在使用 jpm run 来测试一切 - 并且一切都很好。现在,我即将完成我想要完成的任务,所以我想在“普通”
我是一名优秀的程序员,十分优秀!