- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
假设我有一个接口(interface)和一个实现它的类,如下所示:
class IPrinter
{
public:
virtual void print(int i, int base = 10) = 0;
virtual void print(char c) = 0;
virtual void print(char *s) = 0;
virtual void print(float f) = 0;
virtual void print(double d) = 0;
virtual ~IPrinter() = default;
private:
...
}
class Printer : public IPrinter
{
public:
void print(int i, int base = 10) override {...}
void print(char c) override {...}
void print(char *s) override {...}
void print(float f) override {...}
void print(double d) override {...}
private:
...
}
然后我决定添加一个简单的装饰器类,如下所示:
class SwitchablePrinter : public IPrinter
{
private:
IPrinter& _printer;
bool _enabled;
...
public:
SwitchablePrinter(IPrinter& p) :
_printer(p),
_enabled(true)
{
}
void print_enable(bool on) { _enabled = on; }
void print(int i, int base = 10) override
{
if (_enabled)
_printer.print(i, base);
}
void print(char c) override
{
if (_enabled)
_printer.print(c);
}
void print(char *s) override
{
if (_enabled)
_printer.print(s);
}
void print(float f) override
{
if (_enabled)
_printer.print(f);
}
void print(double d) override
{
if (_enabled)
_printer.print(d);
}
}
现在,所有这些都非常简单明了。问题在于 SwitchablePrinter 实现中存在大量代码重复。我想知道,是否有一种方法可以为基类中的所有重载方法编写通用方法“print”,如下所示:
(pseudo-code)
void print({any args})
{
if (_enabled)
_printer.print({any args});
}
我认为可能有一个使用模板的解决方案,但我对使用它们不是很有经验,需要建议。
最佳答案
虽然没有同时覆盖多个成员函数的机制,但您可以通过提供一个私有(private)可变成员函数模板来简化您的任务并减少代码重复,该模板将调用转发到包装的 _printer
,像这样:
private:
template <class ... T>
void print_impl(T ... vals) {
if (_enabled)
_printer.print(vals...);
else
cout << "disabled" << endl;
}
public:
void print(int i, int r) override {
print_impl(i, r);
}
void print(float f) override {
print_impl(f);
}
void print(double d) override {
print_impl(d);
}
void print(char* s) override {
print_impl(s);
}
void print(char c) override {
print_impl(c);
}
这只是在原来的基础上稍作改进,因为print_impl
的转发逻辑非常简单。当逻辑变得更加复杂时,共享代码的返回就会增加。
关于c++ - 如何轻松覆盖多个重载方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33213719/
我尝试将resteasy与自定义对象一起使用,当我创建jar文件时,它与intellij IDE一起工作正常,但失败并出现以下异常 org.jboss.resteasy.core.NoMessageB
我正在寻找一种在easy-close选项为TRUE时基于Shiny模态关闭触发事件的方法(因此,在模态外部单击将其关闭)。由于没有链接到模式的ID,因此我似乎无法捕获此事件。我尝试在“观察”事件中包装
假设我有一些定义如下的类: class Security { Boolean AuthenticateUser(String username, String password); B
正如标题所说,Coq 可以用作模型检查器吗?我可以将模型检查与 Coq 证明混合使用吗?这是常态吗?谷歌谈论“微积分”,有没有人有这方面的经验或类似的经验?是否建议以这种方式使用 Coq,或者我应该寻
是否有一种方法(设置或快捷方式)可以显示输出超过 500 行的查询的总行数 - 即,无需修改首选项中的“结果集页面大小”值?我本质上是在寻找 select count(*) from () t 的输出
我想这样做: System.out.println("안녕하세요!"); 但是当我尝试在 Eclipse 中进行编译时,出现“某些字符无法使用 MacRoman 字符编码进行编码”弹出式错误消息。我正
如果我有一个用这样的字符串初始化的框架 setter CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(at
所以这里没有什么新内容,我只是想得到一些澄清,但似乎在其他帖子中找不到任何澄清。 我正在安静地创建一个新资源,例如: /books (POST) 有一个 body : { title: 'The
我有很多预处理器宏定义,如下所示: #define FOO 1 #define BAR 2 #define BAZ 3 在实际应用中,每个定义对应一个解释器虚拟机中的一条指令。宏的编号也不是连续的,以
使用 SpriteKit 开发 iOS 游戏。我的背景由 map block 组成(本质上是无限 map ,程序生成)。 我们的系统旨在管理 map 的“ block ”,我们只加载玩家附近的 blo
我需要在 Ruby 中拆分一个具有以下格式的字符串: [{a:1,b:2,c:3,d:4},{a:5,b:6,c:7,d:8},{a:9,b:10,c:11,d:12},{a:13,b:14,c:1
Linq 有一个名为 Take() 的便捷运算符方法,可以返回任何实现 IEnumerable 的元素中给定数量的元素。 jQuery 中是否有类似的东西可以处理数组? 或者,换个方式问:如何在 Ja
每当我使用以下代码在文档中插入图像时, var cursor = DocumentApp.getActiveDocument().getCursor(); var image = cursor
今天看到这样一段代码: if ( not defined($reply_address) or not defined($from_name) or not defined(
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
刚刚了解 mercurial 的 --style和 --template可用于 hg log 的选项和 hg tip我发现它们非常有帮助,但我不知道把我的“样式文件”放在哪里 我有一个“样式文件”,它
是否有一些应用程序可以自动 bundle (并缩小)包含 require('file.js') 调用的 JS 项目?这样它们就会合并并生成一个文件。 具体来说,我正在谈论when.js ,一个带有大量
如何(轻松)获取 Sublime Text 3 中的当前文件路径 我不经常使用 ST 控制台(我只使用它一次来安装包管理器),但我认为这可能是一个好方法: 像某种pwd命令一样获取当前文件路径。 但这
嗨,我正在使用resteasy api,我需要使用Map作为QueryParam。我可以使用列表作为 QueryParam,但是当我尝试传递 Map 时,我收到下面提到的错误。 这是我的服务代码 @G
假设我有一个 C++ 代码(请参阅下面的简单示例)。我想让期刊审稿人轻松安装/运行 所以我认为最简单的方法是将其变形为简化的类 R 包的 tar.gz 文件,以便裁判可以安装它通过简单地调用 inst
我是一名优秀的程序员,十分优秀!