C++ 从基类继承了什么?

C++ 从基类继承了什么?,c++,inheritance,C++,Inheritance,摘自这里: 从基类继承了什么? 原则上,派生类继承基类的每个成员,但以下成员除外: its constructor and its destructor its operator=() members its friends 我的问题是,什么是运算符=()成员?运算符=()是赋值运算符。“成员”上的复数表示所有赋值运算符重载(例如,+=,*=等)。运算符=()是赋值运算符。通过在“成员”上使用复数,它意味着所有赋值运算符重载(例如+=,*=,等等)。它可以是赋值运算符对象和运算符=(const

摘自这里:

从基类继承了什么? 原则上,派生类继承基类的每个成员,但以下成员除外:

its constructor and its destructor
its operator=() members
its friends

我的问题是,什么是运算符=()成员?

运算符=()是赋值运算符。“成员”上的复数表示所有赋值运算符重载(例如,
+=
*=
等)。

运算符=()
是赋值运算符。通过在“成员”上使用复数,它意味着所有赋值运算符重载(例如
+=
*=
,等等)。

它可以是赋值运算符
对象和运算符=(const Object&rhs)
和转移运算符
对象和运算符=(Object&rhs)
,如智能指针等所示

可以是赋值运算符
对象和运算符=(常量对象和rhs)
和转移运算符
对象和运算符=(对象和rhs)
,如智能指针等所示。

您可以定义任何类型的运算符=()。即使是非常无用的。但其中的非继承类将继承给子类。从你的链接网站,我改变了一点,使其更清楚的例子。由于上述错误,此示例无法编译

class mother {
public:
    mother ()
    { cout << "mother: no parameters\n"; }
    explicit mother (int a):m_int(a)
    { cout << "mother: int parameter\n"; }

    mother& operator=(mother const& rhs)
    {
        if(&rhs != this)
        {
            m_int = rhs.m_int;
        }
        return *this;
    }

    mother& operator=(int i)
    {
        m_int = i;
        return *this;
    }
private:
    int m_int;
};

class son : public mother {
public:
    explicit  son (int a) : mother (a)
    { cout << "son: int parameter\n\n"; }
};
int main()
{
    mother mom(2);
    son daniel(0);
    mom = 3;
    daniel = 4; // compile error
    daniel = mom; // also error
}
班主任{
公众:
母亲()

{cout您可以定义任何类型的运算符=()。即使是非常无用的运算符。但它们中的任何一个都将继承到子类。从您的链接站点,我对示例进行了一些更改,以使其更清楚。由于上述错误,此示例将无法编译

class mother {
public:
    mother ()
    { cout << "mother: no parameters\n"; }
    explicit mother (int a):m_int(a)
    { cout << "mother: int parameter\n"; }

    mother& operator=(mother const& rhs)
    {
        if(&rhs != this)
        {
            m_int = rhs.m_int;
        }
        return *this;
    }

    mother& operator=(int i)
    {
        m_int = i;
        return *this;
    }
private:
    int m_int;
};

class son : public mother {
public:
    explicit  son (int a) : mother (a)
    { cout << "son: int parameter\n\n"; }
};
int main()
{
    mother mom(2);
    son daniel(0);
    mom = 3;
    daniel = 4; // compile error
    daniel = mom; // also error
}
班主任{
公众:
母亲()

{cout运算符=()
是类赋值运算符,如果您希望能够轻松地将值从类的一个对象赋值到另一个对象,而无需每次都繁琐地通过过程,则可以定义该运算符。该过程称为
重载
,Wikipedia和
运算符都包含了该主题=()
是类赋值运算符,如果您希望能够轻松地将值从类的一个对象赋值到另一个对象,而不必每次都繁琐地通过过程,则可以定义它。该过程称为
重载
,Wikipedia有一个覆盖该主题的函数,我怀疑它的复数形式也可能意味着
运算符=
的多个重载。值得注意的是,对于C++98,该列表仅部分正确。在C++98中,构造函数不是继承的(而
运算符=
是继承的,但会自动隐藏)。在C++11中,必须区分普通继承(不包括构造函数)和通过
使用
的显式构造函数继承。我怀疑复数也可能意味着
运算符=
的多个重载。值得注意的是,该列表对于C++98仅部分正确。在C++98中,构造函数不是继承的(虽然继承了
运算符=
,但会自动隐藏)在C++11中,必须区分普通继承(不包括构造函数)和通过
使用
的显式构造函数继承。试图合理化垃圾cplusplus.com spews是愚蠢的差事!买。试图合理化垃圾cplusplus.com spews是愚蠢的差事!买。