微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用operator =和initializer_list找不到继承

如何解决使用operator =和initializer_list找不到继承

这是我为这个问题简化的代码

template<class T>
class A
{
public:

    A<T>& operator=(A<T> const& other)
    {
        std::cout << "A<T>& operator=(A<T> const& other)\n";
        return this->operator=(other.m_container);
    }

    template<class U>
    A<T>& operator=(std::vector<U> const& other)
    {
        std::cout << "A<T>& operator=(std::vector<U> const& other)\n";
        // Code not shown: m_container = other;
        return *this;
    }

    A<T>& operator=(std::initializer_list<T> il)
    {
        std::cout << "A<T>& operator=(std::initializer_list<T> il)\n";
        // Code not shown: m_container = il;
        return *this;
    }

    std::vector<T> m_container;
};

//
template<typename T>
class B: public A<T>
{};

最初,我认为这是模板的问题,但这不会改变事实。 因此,这里是我的问题的简化类:

class AA
{
public:

    AA& operator=(AA const& other)
    {
        std::cout << "AA& operator=(AA const& other)\n";
        return this->operator=(other.m_container);
    }

    AA& operator=(std::vector<float> const& other)
    {
        std::cout << "AA& operator=(std::vector<float> const& other)\n";
        // Code not shown: m_container = other;
        return *this;
    }

    AA& operator=(std::initializer_list<float> il)
    {
        std::cout << "AA& operator=(std::initializer_list<float> il)\n";
        // Code not shown: m_container = il;
        return *this;
    }

    std::vector<float> m_container;
};

//
class BB: public AA
{};

我的问题是我不能完全确定为什么:

BB bb1;
bb1 = { -1.0f,-1.0f,0.0f };

未编译:

In function ‘int main()’:
error: no match for ‘operator=’ (operand types are ‘BB’ and ‘<brace-enclosed initializer list>’)
     bb1 = { -1.0f,0.0f };
                                ^
note: candidate: ‘BB& BB::operator=(const BB&)’
     BB& operator=(BB const& other)
         ^~~~~~~~
note:   no kNown conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const BB&’

在进行以下修改的同时,代码仍在工作:

class BB: public AA
{
public:

    BB& operator=(std::initializer_list<float> il)
    {
        std::cout << "BB& operator=(std::initializer_list<float> il)\n";
        AA::operator=(il);
        return *this;
    }
};

结果:

BB& operator=(std::initializer_list<float> il)
AA& operator=(std::initializer_list<float> il)

这特别奇怪,因为以下代码不受影响(继承被很好地调用):

BB bb1;
BB bb2;
bb2 = bb1;

结果:

AA& operator=(AA const& other)
AA& operator=(std::vector<float> const& other)

因此无需将类更改为:

class BB: public AA
{
public:

    BB& operator=(BB const& other)
    {
        std::cout << "BB& operator=(BB const& other)\n";
        AA::operator=(other.m_container);
        return *this;
    }

    BB& operator=(std::initializer_list<float> il)
    {
        std::cout << "BB& operator=(std::initializer_list<float> il)\n";
        AA::operator=(il);
        return *this;
    }
};

起初,我认为这是因为我必须从模板派生类中调用base<T>::method(),这就是为什么我必须创建一个调用基数的derived<T>::method()的原因,但这不是原因。理想情况下,我想避免创建对我来说似乎无用的复制/粘贴代码BB& operator=(std::initializer_list<float> il)

谢谢!

解决方法

您的问题是您尝试使用以下方式

BB bb1;

bb1 = { -1.0f,-1.0f,0.0f };

不是当您尝试使用初始化列表初始化BB对象时。错误来自operator=()

为解决您的问题,建议您在using类中添加一个BB,如下所示

class BB: public AA
 {
   public:
      using AA::operator=;
 };

问题在于,BB隐式定义了两个operator=()(副本和移动operator=()BB & BB::operator= (BB const &)BB & BB::operator= (BB &&)),它们隐藏了从AA继承的运算符。

要取消隐藏它们,您必须使用using声明将它们明确纳入范围。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。