类层次结构中的运算符重载
Operator overloads within a class heirarchy
如果之前有人问过并回答过,我很抱歉,但我现在一直在寻找一个可以理解的解决方案几个小时,没有快乐。
我正在尝试实现一个简单的类层次结构,并在基类中定义运算符重载(因为这些在各种派生类之间不会有所不同(。 但是,由于其中大多数都需要返回我们在上下文中的任何派生类的新对象,因此我假设它们需要声明为模板化方法,但是,当我尝试编译这样做时,链接器给了我一个"未解析的外部符号"错误......
举个例子:
/// This is in the class header file
class Base
{
// Declare all the base storage and methods ...
public:
template<class T>
friend T operator+(const T& lhs, const T& rhs);
// .... other stuff in here.
}
/// Then in the implementation file
T operator+ (const T& lhs, const T& rhs)
{
return T(lhs->m_1 + rhs->m_1, lhs->m_2);
}
我希望这会导致我能够声明派生对象,这样:
class Derived : public Base
{
// ... Add the derived class functionality
// -- Question: Do I need to do anything in here with operator+ ???
}
Derived A();
Derived B();
Derived C = A + B;
是所需的结果,但除了在每个单独的派生类中定义运算符外,我看不到可以在C++中实现它的方法,因为模板方法会导致链接器错误。
我是否错过了一些显而易见和基本的东西,或者根本没有简单的方法来做到这一点C++?
您的注释指示模板的"在实现文件中",这可能是导致问题的原因。函数模板声明(例如T operator+(const T&, const&);
声明必须链接的符号 - 但它需要在某处实例化该模板。
简单地在源文件中定义template
函数实际上并不会实例化代码 - 它需要显式希望链接的每种类型的具体实例化,或者使函数模板定义从打算调用它的位置可见(有关更多详细信息,请参阅此答案(。
在许多情况下,最好在头文件中定义函数模板,以便模板的任何调用方都能够实例化函数,而无需链接到其他位置的现有实例化。
可是。。。
您可能需要重新考虑当前的方法。operator+
模板不限制可以考虑的T
类型,这将导致operator+(const T&, const T&)
成为任何尚未具有operator+
的T
类型的可行重载,前提是声明在重载解析期间可见。这可能会导致其他奇怪的编译器/链接器错误。
有几种方法可以解决约束类型;可能最简单的方法是使用 SFINAE 通过检查T
是否派生自Base
来约束它。
例如:
/// This is in the class header file
class Base
{
// Declare all the base storage and methods ...
public:
template<class T, class>
friend T operator+(const T& lhs, const T& rhs);
// .... other stuff in here.
}
// Note: In the same header!
// This only enables + if 'T' derives from 'Base'
template <typename T, typename = std::enable_if_t<std::is_base_of<Base,T>::value>>
T operator+(const T& lhs, const T& rhs)
{
return T(lhs->m_1 + rhs->m_1, lhs->m_2);
}
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- <T> 通过模板化运算符重载将 std::complex 乘以双倍
- C++20概念:需要运算符重载
- 使用赋值运算符重载从类中返回jobject
- 在运算符重载定义中使用成员函数(const错误)
- 字节到位运算符重载C++
- 为什么在运算符重载时需要参考?
- 类中 c++ 的运算符 + 重载
- 算术复合运算符重载为非成员
- 运算符重载 (+),用于添加两个具有 C++ 的数组
- 交换运算符 + 重载会导致无限递归
- 如何理解新的运算符重载?
- 向量保持复数的运算符重载
- 如何创建运算符重载?
- 链接列表运算符重载没有打印出我想要的内容
- C++:需要帮助了解运算符重载错误
- 使用模板化运算符重载 XOR 运算符失败
- 如何确保接受的C++模板类型使运算符重载?
- 运算符重载使用运算符+添加类模板