English 中文(简体)
衍生的班级是否间接继承了基底转让经营者?
原标题:Do derived classes indirectly inherit base s assignment operator?

我试图理解这一行为,但似乎我没有。 请参看该法典:

#include <iostream>
using namespace std;

class Base
{
public:
    void operator=(const Base& rf)
    {
        cout << "base operator=" << endl;
        this->y = rf.y;
    }
    int y;
    Base() : y(100) { }
};

class Derived : public Base
{
public:
    int x;
    Derived() : x(100) { }
};

int main()
{
    Derived test;
    Derived test2;
    test2.x = 0;
    test2.y = 0;
    test.operator=(test2); // operator auto-generated for derived class but...
    cout << test.x << endl << test.y << endl;
    cin.ignore();
    return 0;
}

PROGRAM OUTPUT:

> base operator=
>  0
>  0

Now where I m confused is: The rule says that a derived class never inherits the assigment operator, instead it creates its own operator= however in this example base s operator= gets invoked on the derived class.

第二,我能够明确地以衍生产品类别援引变迁经营人,而这反过来又在衍生产品类别中作了明确规定。

现在,如果我正确理解,这意味着任何用户界定的基地运营商总是在衍生类别上援引?

最佳回答

产生者自动 查询<>。

// generated version looks basically like this
Derived& operator=(Derived const& other){
  Base::operator=(static_cast<Base const&>(other));
  x = other.x;
  return *this;
}

The cast is there to avoid an accidential call to a templated Base::operator= like this one:

template<class Other>
Base& operator=(Other const& other); // accepts everything

或这样奇怪:

// forward-declare  Derived  outside of  Base 
Base& operator=(Derived const& other); // accepts derived class (for whatever reason)

第二,我能够明确地以衍生产品类别援引变迁经营人,而这反过来又在衍生产品类别中作了明确规定。

如果您不和你的班级允许,编辑员自动宣布<>>指定操作员(即没有参考成员和其他某些电弧规则),如果你实际使用,则自动公布<>。

问题回答

汇编者生成的转让操作者将每一次标的转让操作人召集起来。 这包括基级和非统计成员变量。

标准如下(第12.8节:<代码>[类别.copy]):

如果类别定义没有明确申报一份复印转让操作人,则默示宣布一份。 如果班级定义宣布移动建筑或移动转让经营人,则间接申报的复印转让经营人的定义为删除;否则,定义为违约(8.4)。 如果班级有用户申报的影印件或用户申报的脱轨器,则对后一情况进行解释。 隐含申报的类别(X)的复印机操作员将拥有表格。

X&  X::operator=(const  X&)

如果有

  • each direct base class B of X has a copy assignment operator whose parameter is of type const B&, const volatile B& or B, 以及
  • for all the non-static data members of X that are of a class type M (or array thereof), each such class type has a copy assignment operator whose parameter is of type const M&, const volatile M& or M.

否则,含蓄申报的复印机投递商将拥有形式

X&  X::operator=(X&)

以及

The implicitly-defined copy/move assignment operator for a non-union class X performs memberwise copy/move assignment of its subobjects. The direct base classes of X are assigned first, in the order of their declaration in the base-spec如果有ier-list, 以及 then the immediate non-static data members of X are assigned, in the order in which they were declared in the class definition. Let x be either the parameter of the function or, for the move operator, an xvalue referring to the parameter. Each subobject is assigned in the manner appropriate to its type:

  • 如果有 the subobject is of class type, as 如果有 by a call to operator= with the subobject as the object expression 以及 the corresponding subobject of x as a single function argument (as 如果有 by explicit qualification; that is, ignoring any possible virtual overriding functions in more derived classes);
  • 如果有 the subobject is an array, each element is assigned, in the manner appropriate to the element type;
  • 如果有 the subobject is of scalar type, the built-in assignment operator is used.

It is unspec如果有ied whether subobjects representing virtual base classes are assigned more than once by the implicitly-defined copy assignment operator.

4 things never get inherited Constructor Copy-constructor Assignment operator Destructor

Even you have not written the assignment operator your code will be Woking fine. Whenever you use assignment operator compiler will make sure that assignment operator for each member object and base class get called.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?