gpt4 book ai didi

c++ - 使用 "this"作为参数复制构造函数

转载 作者:行者123 更新时间:2023-12-02 05:45:53 24 4
gpt4 key购买 nike

我有一个 C++ 类,假设它名为 c,我想在其中一个方法中使用隐式复制构造函数,如下所示:

c c::do_something() {
c copy = this; //I want implicit copy constructor here!
copy.something_else();
//........//
return copy;
}

但是,gcc 返回此错误:

error: invalid conversion from 'c* const' to 'long unsigned int'

(我有另一个来自 long unsigned int 的构造函数)

...就好像复制构造函数不存在一样。我做错了什么?

最佳答案

this是一个指向对象的指针,所以它应该是

c copy = *this;

关于c++ - 使用 "this"作为参数复制构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/555108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com