它会调用哪个函数?

发布于 2024-11-14 19:46:03 字数 2489 浏览 0 评论 0原文

这是代码,我写了注释。 问题是我不知道 Derive 类中的函数取消隐藏后将调用哪个函数。

    #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-statement会这样解释

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;

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

当我编写:

Base b1;
derive1 = b1;

function Base& 时,最终将调用 Base::operator=(const Base &b);

当我编写:

derive2 = derive1;

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

Here is the code, I wrote the comments.
The question is I don't know which function will be called after the function unhide in the Derive class.

    #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;
    }

But the result of the code is: derive2.num equals to 1!!!, that means the whole class has been copied after the statment, why would this happen?

Thanks to Tony, I think I got the answer.

here is my explanation:

Based on C++0x 7.3.3.3 and 12.8.10, The using-statement in Derive will be explained like this

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
};

So when I wrote:

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

function Base& Base::operator=(string& str); will be called,

and when I wrote:

Base b1;
derive1 = b1;

function Base& Base::operator=(const Base &b); will be called,

finnaly, when I wrote:

derive2 = derive1;

function Derive& Dervie::operator=(const Derive&); will be called.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

带刺的爱情 2024-11-21 19:46:03

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

如果从基类带入派生类作用域的赋值运算符具有派生类 (class.copy) 的复制赋值运算符的签名,则 using 声明本身不会禁止隐式声明派生类复制赋值运算符;基类中的复制赋值运算符被派生类的隐式声明的复制赋值运算符隐藏或覆盖,如下所述。

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

Standard 7.3.3-4 (from an old draft, but in this regard still valid):

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.

So, the implicit Derived::operator=() is used.

瀟灑尐姊 2024-11-21 19:46:03

它将调用派生的 operator=,它在自动生成的实现中将从 Base 调用 operator= 并复制 中的成员>推导

It will call the derived operator=, which in its automatically generated implementation will call operator= from Base as well as copy the members in Derive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文