c++ 中的复制构造函数和运算符 =

发布于 2024-12-05 13:36:37 字数 509 浏览 0 评论 0原文

我对以下代码有疑问:

假设我有一个类 P,它有一个复制构造函数和一个接收一个字符串值的常规构造函数。

我有以下代码:

P doSomething(){
   P p("myValue");
   return p;
}

int main(){
    P m=doSomething();
    return 1;
}
  1. 为什么在 doSomething() 函数的 return p 处不调用复制构造函数?
  2. 调用 P m=doSomething() - 它是否假设调用复制构造函数或运算符=?
  3. 如果是运算符=,这段代码和下面的有什么区别:

    P new_val=("newVal");
    pm=new_val;
    

我知道这里的调用是针对复制构造函数的)

谢谢, 玛丽

I have a question about the following code:

let's say I have a class P that has a copy constructor and a regular constructor the receives one string value.

I have the following code:

P doSomething(){
   P p("myValue");
   return p;
}

int main(){
    P m=doSomething();
    return 1;
}
  1. why isn't copy constructor invoked at the return p of the doSomething() function?
  2. the call P m=doSomething() - does it suppose to call the copy constructor or the operator=?
  3. in case it's operator =, what is the difference of this code and the following:

    P new_val=("newVal");
    p m=new_val;
    

(i know here the call is for copy constructor)

Thanks,
Mary

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

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

发布评论

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

评论(3

孤星 2024-12-12 13:36:37
  • 为什么在 doSomething() 函数的返回 p 处不调用复制构造函数?

该标准允许删除该副本。谷歌搜索[N]RVO。在某些编译器上,这种情况仅在优化时发生,而在其他编译器上,它是调用约定的一部分,因此总是会发生。

  • 调用 P m=doSomething() - 它是否假设调用复制构造函数或运算符=?

T t = x;T t(T(x)) 的“语法糖”(在 T(x) 隐式发生的意义上),因此尽管有 =,但与 operator= 无关。请注意,这里附加的临时文件也可能被省略,因此不会调用复制器。

  • 如果是运算符=,这段代码和下面的有什么区别:

这段代码毫无意义,你真正的意思是什么?

P new=("newVal");
p m=new;
  • why isn't copy constructor invoked at the return p of the doSomething() function?

The standard allows this copy to be elided. Google for [N]RVO. On certain compilers this happens only when optimizing, on others it is part of the calling conventions and thus happens always.

  • the call P m=doSomething() - does it suppose to call the copy constructor or the operator=?

T t = x; is "syntactic sugar" (in the sense that the T(x) is happening implicitly) for T t(T(x)) and thus has -- despite the = -- nothing to do with operator=. Note that also here the additional temporary may be elided, thus no copy ctor is called.

  • in case it's operator =, what is the difference of this code and the following:

This code makes no sense, what did you really mean?

P new=("newVal");
p m=new;
尹雨沫 2024-12-12 13:36:37

我做了一个小样品用于演示。

void print(char* text, int ident)
{
    for(int i = 0 ; i < ident ; i++)
    {
        printf("   ");
    } // end for

    fprintf(stdout, text);
    fprintf(stdout, "\n");
    fflush(stdout);
}

class P
{
public:
    char* _str;

    P (P& arg)
    {
        print("copy constructor", 2);
        arg._str = this->_str;
    }

    P (char* str)
    {
        print("constructor", 2);
        _str = str;
    }
};

P doSomething(){
    print("do something - function", 1);
    P p("myValue");
    print("before return p", 1);
    return p;
}

int main(int argc, char* argv[])
{
    print("start", 0);
    P m=doSomething();
    print("stop - call return", 0);
    return 1;
}

这会返回,

start
   do something - function
      constructor
   before return p
      copy constructor
stop - call return

因此复制构造函数将被调用

I made a small sample for demonstration.

void print(char* text, int ident)
{
    for(int i = 0 ; i < ident ; i++)
    {
        printf("   ");
    } // end for

    fprintf(stdout, text);
    fprintf(stdout, "\n");
    fflush(stdout);
}

class P
{
public:
    char* _str;

    P (P& arg)
    {
        print("copy constructor", 2);
        arg._str = this->_str;
    }

    P (char* str)
    {
        print("constructor", 2);
        _str = str;
    }
};

P doSomething(){
    print("do something - function", 1);
    P p("myValue");
    print("before return p", 1);
    return p;
}

int main(int argc, char* argv[])
{
    print("start", 0);
    P m=doSomething();
    print("stop - call return", 0);
    return 1;
}

this returns

start
   do something - function
      constructor
   before return p
      copy constructor
stop - call return

so copy constructor WILL BE CALLED

孤独患者 2024-12-12 13:36:37

1) 为什么在 doSomething() 函数的“return p”处不调用复制构造函数?

它应该 - 尝试在复制器中的控制台上打印一些内容,看看它是否真的在运行。

2) 调用 P m=doSomething() - 它是否假设调用复制构造函数或运算符=?

应该调用运算符=。再次,使用方法内的调试消息打印来检查

3)如果是运算符=,这段代码和下面的代码有什么区别:
P new=("newVal");下午=新; (我知道这里的调用是复制构造函数)

您错过了代码片段中的某些内容吗?我认为它不会编译

1) why isn't copy constructor invoked at the "return p" of the doSomething() function?

it should - try printing something to the console in the copy c-tor to see if it is really running.

2) the call P m=doSomething() - does it suppose to call the copy constructor or the operator=?

should call the operator=. again, use the debug message print from within the method to check

3) in case it's operator =, what is the difference of this code and the following:
P new=("newVal"); p m=new; (i know here the call is for copy constructor)

did you miss something on the code snippet? i think it won't compile

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