gpt4 book ai didi

c++ - 重载模板类的赋值运算符

转载 作者:行者123 更新时间:2023-11-28 03:17:56 27 4
gpt4 key购买 nike

我正在尝试为两个不同但具有相同模板类型的模板类重载赋值运算符:

template <class DataType> class Foo
{
public: Foo<data_type>& operator=(Bar<data_type> const &bar);
};
template <class DataType> class Bar
{
public: Bar<data_type>& operator=(Foo<data_type> const &foo);
};

但是当我尝试时:

Foo<int> a;
Bar<int> b = a;

我得到错误:

No viable conversion from 'Foo< int >' to 'Bar< int >'.

我如何实现这一目标?

最佳答案

转换是通过复制构造函数完成的,而不是赋值运算符。所以你想实现:

Bar(const Foo<data_type>& foo);

关于c++ - 重载模板类的赋值运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16266608/

27 4 0
文章推荐: c++ - 在 vector 中添加字符串数字时出错
文章推荐: javascript - 弱映射内存泄漏
文章推荐: HTML 网站在发布后进行奇怪的优化?
文章推荐: html - 如何将 转换为
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com