gpt4 book ai didi

c++ - 从模板派生

转载 作者:太空狗 更新时间:2023-10-29 21:07:02 26 4
gpt4 key购买 nike

我坚持以下几点,需要一些帮助:

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...
}

看起来派生类没有得到重载运算符

最佳答案

试试这个:

class Evolved : public PrimativeUS
{
public:
using PrimativeUS::operator=;
Evolved() {}
};

为您提供的隐式 Evolved::operator=(const Evovled&) 隐藏了基类中存在的所有 operator= 实例。 (任何方法都是如此 - 派生类的方法隐藏了基类的类似命名方法,即使签名不匹配也是如此。)

关于c++ - 从模板派生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5981008/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com