c++ - 它会调用哪个函数?

标签 c++ inheritance operators operator-overloading

这是代码,我写了注释。 问题是我不知道在Derive类中函数unhide后会调用哪个函数。

    #include <CONIO.H>
    #include <IOSTREAM>
    #include <string>

    using namespace std;

    class Base
    {
        string strName;
    public:
        Base& operator=(const Base &b)
        {
            this->strName = b.strName;
            cout << "copy assignment" << endl;
            return *this;
        }
        Base& operator=(string& str)
        {
            this->strName = str;
            cout << "operator=(string& str)" << endl;
            return *this;
        }

    };

    class Derive : public Base
    {
    public:
        int num;
        using Base::operator =; // unhide Base::operator=();
    };

    int main(int argc, char *argv[])
    {
        Derive derive1;
        derive1.num = 1;

        Derive derive2;

        Base b1;
        derive1 = b1;  // This will call Base& Base::operator=(const Base &b)
                           //no problem

        string str("test");
        derive1 = str;  // This will call Base& Base::operator=(string& str)
                            // no problem

        derive2 = derive1; // What function will this statement call???
                               // If it calls Base& Base::operator(const Base &b)
                               // how could it be assigend to a class Derive?
        return 0;
    }

但是代码的结果是:derive2.num等于1!!!,也就是说语句之后整个类都被复制了,为什么会这样呢?

多亏了托尼,我想我找到了答案。

这是我的解释:

基于C++0x 7.3.3.3和12.8.10,Derive中的using语句会这样解释

class Derive : public Base
{
public:
    int num;
    //using Base::operator =;
    Base& operator=(const Base &b); // comes form the using-statement
    Base& operator=(string& str); // comes form the using-statement
    Derive& operator=(const Derive &); // implicitly declared by complier
};

所以当我写的时候:

string str("test");
derive1 = str;

函数 Base& Base::operator=(string& str); 将被调用,

当我写的时候:

Base b1;
derive1 = b1;

函数 Base& Base::operator=(const Base &b); 将被调用,

最后,当我写道:

derive2 = derive1;

函数 Derive& Dervie::operator=(const Derive&); 将被调用。

最佳答案

标准 7.3.3-4(来自旧草案,但在这方面仍然有效):

If an assignment operator brought from a base class into a derived class scope has the signature of a copy-assignment operator for the derived class (class.copy), the using-declaration does not by itself suppress the implicit declaration of the derived class copy-assignment operator; the copy-assignment operator from the base class is hidden or overridden by the implicitly-declared copy-assignment operator of the derived class, as described below.

因此,使用隐式 Derived::operator=()

关于c++ - 它会调用哪个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6275383/

相关文章:

python - 微分方程的解在 boost::odeint 和 scipy.integrate 中完全不同

c++ - 如何在 C++ 中定义文件范围符号

Java - 如何根据继承类的构造函数参数调用不同的 super()?

java - 调用从 main 方法扩展另一个类的类

C++:重载运算符=

c++ - 使用两种方法覆盖 << 运算符

c++ - 使用自由函数作为伪构造函数来利用模板参数推导

C++:具有未知删除器的 unique_ptr

javascript - 为什么 getter 或 setter 在 JavaScript 中不能独立继承?

c++ - 为什么表达式 a = a + b - ( b = a ) 在 c++ 中给出序列点警告?