模板继承和运算符
Templates inheritance and operators
我有以下代码,当我在属性实例上调用 operator=() 时会导致问题:
// myProperty.h.
template <class T, int typeFamily = TypeFamily<T>::value>
class PropertyImpl : public PropertyBase
{
// Default template to catch errors.
};
template <class T>
class PropertyImpl<T, 0> : public PropertyBase
{
// Data is part of the family of base types.
public:
PropertyImpl(T* dataRef) : PropertyBase(), m_dataRef(dataRef) {}
void operator=(T const & data) {*m_dataRef = data;}
protected:
T* m_dataRef;
};
template <class T>
class Property : public PropertyImpl<T> {};
请注意,TypeFamily<>
是一些元代码计算 T 是否为受支持的基类型。 如果 T 是浮点数,则TypeFamily<T>::value
为 0。
现在我创建一个聚合属性
// myNode.h.
class myNode
{
public:
void setProp(float val) {m_prop = val;}
protected:
Property<float> m_prop;
}
我最初认为Property<float>
从PropertyImpl<float, 0>
派生出来,我将能够调用m_prop = val
,因为operator=()
是为PropertyImpl<float, 0>
定义的。但是我的编译器返回以下错误:
<myNode_path>(myNode_line) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'float' (or there is no acceptable conversion)
3> <myProperty_path>(myProperty_line): could be 'Property<T> &Property<T>::operator =(const MPC::Property<T> &)'
3> with
3> [
3> T=float
3> ]
3> while trying to match the argument list '(Property<T>, float)'
3> with
3> [
3> T=float
3> ]
这对我来说是完全不清楚的,我感觉我错过了模板的基本行为。或者它很容易在我眼前捕捉......
有人明白发生了什么吗?
谢谢!
Property
包含一个隐式声明的复制赋值运算符,该运算符隐藏基类中的运算符。您需要一个 using 声明才能使其可访问:
template <class T>
class Property : public PropertyImpl<T> {
public:
using PropertyImpl<T>::operator=;
};
相关文章:
- 通过继承类使用来自不同命名空间的运算符
- 运算符继承和 cpp 核心准则 c.128 的问题
- 如果我真的真的想从 STL 容器继承,并且我继承构造函数并删除新运算符,会发生什么?
- 继承模板化转换运算符
- C++ 继承运算符=
- 在继承的模板类中使用下标 [] 运算符
- 如何调用继承的重载运算符<<并在派生类的输出中添加更多文本?
- C++模板化类上继承的运算符'<<'
- 类继承和运算符重载
- 通过继承重载运算符会导致歧义
- 使用继承的指针列表复制构造函数或重载运算符=
- C++中移动赋值运算符的继承
- 如何在C++上的多个继承上下文上使用特定基类中的运算符
- 继承类中的比较运算符
- 继承 C++14 中的模板化运算符 =:g++ 和 clang++ 的不同行为
- 具有继承的未声明标识符(运算符 ostream)
- 如何使用运算符编写可继承的模板类
- C++运算符继承和重载
- 重载的强制转换运算符继承(Visual C++)
- 赋值运算符继承