简体   繁体   English

子类化,赋值运算符和副本构造函数

[英]Subclassing, assignment operator and copy constructor

I have a bunch of questions related to this, I couldn't find exact answers. 我有很多与此相关的问题,我找不到确切的答案。 Class A is the main class, and B is a subclass A类是主要类,B是子类

  1. If A::operator=(const A & other) was defined, does the default implementation of B::operator= copies members of B then calls A::operator= ? 如果定义了A :: operator =(const A&other),则B :: operator =的默认实现会复制B的成员,然后调用A :: operator =吗?

  2. If A copy constructor was defined, does the default implementation of the copy constructor for B copy-constructs members of B, then calls A copy constructor? 如果定义了A复制构造函数,那么B的复制构造函数的默认实现是否会复制B的成员,然后调用A复制构造函数?

  3. Do I need to define above functions virtual in A to get this behavior? 我是否需要在A中虚拟定义上述函数才能获得此行为? (I suppose yes for operator= and no for the copy constructor, as virtual constructors is a nonsense?) (我想对operator =是,对于复制构造函数则为no,因为虚拟构造函数是胡说八道?)

  4. Can I forbid overloading the assignment operator or the copy constructor for subclasses of A, to force the use of default implementations? 我可以禁止为A的子类重载赋值运算符或复制构造函数,以强制使用默认实现吗?

The idea behind this, being to offer a plugin API for my users. 其背后的想法是为我的用户提供插件API。 I need the API to be C++ as scripts are too slow (I'll try JIT compilation one day), but it should be very simple. 我需要API为C ++,因为脚本太慢(我将有一天尝试JIT编译),但这应该非常简单。

  1. If the default copy-assignment operator of class B is not deleted, [class.copy]/28 如果未删除类别B的默认副本分配运算符,则[class.copy] / 28

    The implicitly-defined copy/move assignment operator for a non-union class X performs memberwise copy-/move assignment of its subobjects. 非联合类X的隐式定义的复制/移动分配操作符对其子对象执行成员式复制/移动分配。 The direct base classes of X are assigned first, in the order of their declaration in the base-specifier-list [ie in the order in which they're listed after class X : /*here*/ {/*...*/}; X的直接基类首先按照它们在base-specifier-list中的声明顺序分配[即,按它们在class X : /*here*/ {/*...*/};之后的列出顺序class X : /*here*/ {/*...*/}; ], and then the immediate non-static data members of X are assigned, in the order in which they were declared in the class definition. ],然后按在类定义中声明它们的顺序分配X的立即非静态数据成员。

  2. Similarly, [class.copy]/15 同样,[class.copy] / 15

    The implicitly-defined copy/move constructor for a non-union class X performs a memberwise copy/move of its bases and members. 非联合类X的隐式定义的复制/移动构造函数执行其基和成员的成员复制/移动。

    The order is: first the base class(es) (base class subobjects), then the direct non-static data members, similarly to 1). 顺序为:首先是基类(基类的子对象),然后是直接的非静态数据成员,类似于1)。

  3. For the behaviour described in 1) and 2), no . 对于1)和2)中描述的行为, Virtual assignment operators are hardly ever useful. 虚拟赋值运算符几乎不再有用。 Constructors may not be virtual at all (doesn't make sense). 构造函数可能根本不是虚拟的(没有任何意义)。

    In order for a virtual function in a derived class B to override a virtual function in a base class A , it must have the same parameter types. 为了使派生类B中的虚函数覆盖基类A的虚函数,它必须具有相同的参数类型。 That is, you could have a virtual A& operator=(A const&); 也就是说,您可以有一个virtual A& operator=(A const&); in the base class A , but the override in class B had to look like virtual B& operator=(A const&); 在基类A ,但类B的替代必须看起来像virtual B& operator=(A const&); , which is not a copy-assignment operator for B , because of the parameter type. ,由于参数类型,它不是 B的副本分配运算符。

  4. Not without "hacks". 并非没有“骇客”。 And you're not actually overloading it, but hiding all base class assignment operators. 而且您实际上并没有使它过载,而是隐藏了所有基类赋值运算符。 Otherwise, this would be legal: 否则,这将是合法的:

     class A {}; class B { int i; }; A a; B b = a; // using A::operator=(A const&) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM