使用“使用”继承基类的复制和移动构造函数。关键词 [英] Inheriting copy and move constructors of base class using "using" keyword

查看:144
本文介绍了使用“使用”继承基类的复制和移动构造函数。关键词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用并使用关键字继承基类的副本构造函数:

I want to inherit copy constructor of the base class using using keyword:

#include <iostream>

struct A
{
    A() = default;

    A(const A  &) { std::cerr << __PRETTY_FUNCTION__ << std::endl; }
    A(      A &&) { std::cerr << __PRETTY_FUNCTION__ << std::endl; }

    A& operator=(const A  &) { std::cerr << __PRETTY_FUNCTION__ << std::endl; return *this; }
    A& operator=(      A &&) { std::cerr << __PRETTY_FUNCTION__ << std::endl; return *this; }
};

struct B : A
{
    using A::A;
    using A::operator=;

    B& operator=(const B  &) { std::cerr << __PRETTY_FUNCTION__ << std::endl; return *this; }
    B& operator=(      B &&) { std::cerr << __PRETTY_FUNCTION__ << std::endl; return *this; }
};

int main()
{
    A a;
    B b;
    b = a; // OK
    B b1(          a ); // compile error
    B b2(std::move(a)); // compile error
    return 0;
}

使用并使用关键字可以正常工作,但是继承复制和移动构造方法会导致编译错误:继承的构造方法不是从相同或派生类型的表达式初始化的候选对象。

Inheriting assignment operator using using keyword works OK, but inheriting copy and move constructors causes a compilation error: an inherited constructor is not a candidate for initialization from an expression of the same or derived type.

http://coliru.stacked-crooked.com/a/ fe84b429c391c894

main.cpp:16:14: note:   an inherited constructor is not a candidate for initialization from an expression of the same or derived type
main.cpp:8:5: note: candidate: A::A(A&&)
     A(      A &&) { std::cerr << __PRETTY_FUNCTION__ << std::endl; }
     ^
main.cpp:16:14: note:   inherited here
     using A::A;

为什么我可以继承赋值运算符但不能继承复制构造函数?有什么区别?我可以理解是否也不能继承赋值运算符。但是相反地,继承赋值运算符被认为是可以的。对我来说有点奇怪。

Why can I inherit assignment operator but cannot inherit copy constructor? What is a difference? I could understand if I couldn't inherit assignment operators too. But inheriting assignment operators in contrary is considered OK. That is a little strange for me.

故事

我想要什么类似于问题中的要求:我想仅向现有类中添加新方法而无需对其进行修改(这是一个

What I want is similar to what is asked in this question: I want to just add new methods to existing class without modifying it (it's a class from another library).

http ://coliru.stacked-crooked.com/a/149a6194717cd465

#include <iostream>

struct A // not my class
{
};

struct B : A
{
    using A::A;
    using A::operator=;

    void foo() { std::cerr << "fuu" << std::endl; }
};

A NotMyFunc()
{
    return {};
}

int main()
{
    B b(NotMyFunc());
    b.foo();
    return 0;
}

但是我不想重新实现复制并移动构造函数。

But I don't want to reimplement copy and move constructors.

推荐答案

您需要使用A作为参数的B的构造函数。

You need a constructor for B that has A as parameter. Then you need to make the default constructor explicit.

struct B : A
{
    using A::A;
    using A::operator=;

    B() = default;
    B(const A& a) : A(a) {}
    B(A &&a): A(std::move(a)) {}
};

这篇关于使用“使用”继承基类的复制和移动构造函数。关键词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆