对特殊情况行为使用继承或模板
Using inheritance or templates for special case behavior
我有两个类A
和B
,它们是具有operate()
方法的Operator
类的子类。
class Operator {
// ...
virtual void operate() = 0;
};
class A : public Operator {
void operate() { /* ... */ }
};
class B : public Operator {
void operate() { /* ... */ }
};
我想创建一个执行两个操作的类,但当两个操作都是B
类型时,它具有唯一的行为。类似的东西
class DoubleOperator {
Operator* operator1;
Operator* operator2;
// ...
public:
void operateTwice() {
if (/* operator1 and operator2 are of type B */) {
// Do a special combined operation.
} else { // Otherwise, do the operations separately.
operator1->operate();
operator2->operate();
}
}
};
有没有一种简单的方法可以使用继承或模板来产生这种行为?
您可以键入带有中间函数和一位SFINAE的擦除运算符
它遵循一个最小的工作示例(C++14,很容易转换为C++11(:
#include<type_traits>
#include<iostream>
class Operator {
// ...
virtual void operate() = 0;
};
class A : public Operator {
void operate() { /* ... */ }
};
class B : public Operator {
void operate() { /* ... */ }
};
class DoubleOperator {
template<typename T, typename U>
static
std::enable_if_t<std::is_same<T,U>::value>
proto() {
std::cout << "same" << std::endl;
// Do a special combined operation.
}
template<typename T, typename U>
static
std::enable_if_t<not std::is_same<T,U>::value>
proto() {
std::cout << "not same" << std::endl;
// Otherwise, do the operations separately.
}
Operator* operator1;
Operator* operator2;
void(*internalOperatorTwice)(void);
// ...
public:
template<typename T, typename U>
DoubleOperator(T *t, U *u): operator1{t}, operator2{u} {
internalOperatorTwice = &proto<T, U>;
}
void operateTwice() {
internalOperatorTwice();
}
};
int main() {
A a1;
A a2;
B b;
DoubleOperator do1{&a1, &a2};
do1.operateTwice();
DoubleOperator do2{&a1, &b};
do2.operateTwice();
}
您可以根据需要相应地扩展/修改proto
。
编辑
我又读了一遍这个问题,下面是对答案的提炼
我上面说的仍然有效。无论如何,如果您只想在有两个B
实例时执行不同的操作,则可以使用以下proto
函数:
template<typename T, typename U>
static
std::enable_if_t<std::is_same<T,B>::value and std::is_same<U,B>::value>
proto() {
std::cout << "same" << std::endl;
// Do a special combined operation.
}
template<typename T, typename U>
static
std::enable_if_t<not (std::is_same<T,B>::value and std::is_same<U,B>::value)>
proto() {
std::cout << "not same" << std::endl;
// Otherwise, do the operations separately.
}
你要问的是:
如何检查Derived
是否为静态类型Base&
的变量的动态类型
struct Base
{
virtual void f(){}
};
struct Derived:Base{};
Derived d;
Base& b = d;
if (typeid(b)==typeid(Derived))
{
//...
}
说明:
5.2.8/2说:
当typeid应用于类型为多态类类型(10.3(的glvalue表达式时,结果引用到一个std::typeinfo对象,该对象表示最派生对象(1.8(的类型(即动态类型(。[…].
因此,我们通过添加virtual
函数来生成Base
多态类类型,并应用typeid
,然后返回动态类型。
相关文章:
- 在双重继承的情况下如何处理非标准构造函数
- 循环依赖,在继承类的情况下使用覆盖方法
- 是否可以在不修改父类的情况下将成员变量初始化推迟到继承的类?
- 类成员在继承的情况下用作模板参数
- c++ 中的函数重载如何在没有钻石继承的情况下工作?
- 虚拟继承情况下类的意外大小
- 虚拟继承情况下的 vtable
- 有什么理由在没有继承的情况下声明一个虚拟方法
- 在我的情况下,多重继承是一种好的设计模式吗?
- 模板和unique_ptr继承情况下的重载解决方案
- 默认情况下,我如何在不使用继承的情况下使用所有构造函数
- 在这种情况下,继承类如何工作??.
- 在多个继承的情况下,C 编译器如何处理成员变量内存偏移
- 在 c++ 中类继承的情况下强制延迟方法解析
- 如何在不继承手柄的情况下分叉处理
- C++:在不是所有派生类都预先已知的情况下使用继承
- 访问受保护的变量 - 继承和子类的复杂情况
- 如何在不使用继承的情况下'rename'模板实例化?
- 虚拟继承情况下类的大小
- 多继承情况下的构造函数