从模板派生
我陷入了以下困境,需要一些帮助:
typedef unsigned short USHORT;
template <typename DataType>
class Primative
{
protected:
DataType m_dtValue;
public:
Primative() : m_dtValue(0) {}
DataType operator=(const DataType c_dtValue) { return m_dtValue = c_dtValue; }
DataType Set(const DataType c_dtValue){ return m_dtValue = c_dtValue; }
};
typedef Primative<USHORT> PrimativeUS;
class Evolved : public PrimativeUS
{
public:
Evolved() {}
};
int main()
{
PrimativeUS prim;
prim = 4;
Evolved evo;
evo.Set(5); // good
evo = USHORT(5); // error, no operator found which takes a right-hand operator...
}
看起来派生类没有获得重载运算符
I'm stuck on the following and could use some help:
typedef unsigned short USHORT;
template <typename DataType>
class Primative
{
protected:
DataType m_dtValue;
public:
Primative() : m_dtValue(0) {}
DataType operator=(const DataType c_dtValue) { return m_dtValue = c_dtValue; }
DataType Set(const DataType c_dtValue){ return m_dtValue = c_dtValue; }
};
typedef Primative<USHORT> PrimativeUS;
class Evolved : public PrimativeUS
{
public:
Evolved() {}
};
int main()
{
PrimativeUS prim;
prim = 4;
Evolved evo;
evo.Set(5); // good
evo = USHORT(5); // error, no operator found which takes a right-hand operator...
}
It looks like the derived class is not getting the overloaded operator
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
为您提供的隐式
Evolved::operator=(const Evovled&)
隐藏了基类中存在的operator=
的所有实例。 (任何方法都是如此 - 派生类的方法隐藏基类的类似命名的方法,即使签名不匹配。)Try this:
The implicit
Evolved::operator=(const Evovled&)
that is provided for you hides all instances ofoperator=
present in the base class. (This is true of any method - methods of derived classes hide similarly-named methods of base class, even if the signatures don't match.)稍微改变你的函数声明:
注意 a &运算符重载需要(参考)符号。
Change your function declaration slightly:
Note that a & (reference) sign is needed for operator overloading.