C++:关于复制构造函数的问题

发布于 2024-09-13 11:01:34 字数 1000 浏览 6 评论 0原文

我有一个类,我使用 this 来初始化 void* 指针。但问题是,当我按值传递该类的实例时,指针不会更改为堆栈上的新地址。因此,我想重写复制构造函数,以使用 this 的新地址重新分配指针。但是我需要变量来调用超类构造函数,我通常通过构造函数参数获取它们,但我的复制构造函数中没有它们......

我希望我解释得很好......

所以问题是:我可以吗保留默认复制构造函数的过程,在其中添加重新分配指针的部分?或者有更好的选择吗?

谢谢

这是一些代码:

class GetsCopiedByValue : public SuperClass
{
    GetsCopiedByValue(Type1 *t1, Type2 *t2, float var1, /* A lot of other things I need */) :
         SuperClass(t1, t2), var1(var1)
    {
         t1->set_GetsCopiedByValue_Address(this);  // ***
    }
}

其他地方:

{
    GetsCopiedByValue instance (t1, t2, 4.64, /* All the other things */);
    SomeType *t = new SomeType(instance); // <-- here, it gets copied by value,
}  // but the original "instance" goes (here) out of scope and the address 
   // I assigned (***) is not longer valid, so I have to
   // reassign the address to the new address, after copying it.

I have a class where I use this to initialize a void* pointer. But the problem is, when I pass an instance of that class by value, the pointer will not change to the new address on the stack. So, I thought to override the copy constructor to reassign the pointer with the new address of this. But I need variables to call the super-class constructor, which I normally get by constructor-parameters, but I don't have them in my copy constructor...

I hope I explained well...

So the question is: can I keep the procedure of the default copy-constructor, where I add the part of reassigning the pointer? Or are there better alternatives?

Thanks

Here is a some code:

class GetsCopiedByValue : public SuperClass
{
    GetsCopiedByValue(Type1 *t1, Type2 *t2, float var1, /* A lot of other things I need */) :
         SuperClass(t1, t2), var1(var1)
    {
         t1->set_GetsCopiedByValue_Address(this);  // ***
    }
}

Somewhere else:

{
    GetsCopiedByValue instance (t1, t2, 4.64, /* All the other things */);
    SomeType *t = new SomeType(instance); // <-- here, it gets copied by value,
}  // but the original "instance" goes (here) out of scope and the address 
   // I assigned (***) is not longer valid, so I have to
   // reassign the address to the new address, after copying it.

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

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

发布评论

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

评论(2

各自安好 2024-09-20 11:01:34

不确定这是否有帮助,但您实际上可以在复制构造函数中调用超类复制构造函数:

GetsCopiedByValue(GetsCopiedByValue const& other) :
     SuperClass(other), var1(other.var1)
{
     t1->set_GetsCopiedByValue_Address(this);
}

但我认为即使您省略此基类构造函数调用,C++ 也会为您插入它。

Not sure if this helps, but you can actually call the superclass copy constructor in your copy constructor:

GetsCopiedByValue(GetsCopiedByValue const& other) :
     SuperClass(other), var1(other.var1)
{
     t1->set_GetsCopiedByValue_Address(this);
}

But I think that even if you omit this base-class constructor call, C++ will insert it for you.

紫瑟鸿黎 2024-09-20 11:01:34

当您第一次构造一个对象时(可能在超类中,因为这就是使用它们的对象),您必须存储参数,然后在复制构造中再次使用它们,即从您正在复制的对象中读回它们。

编辑

更好的是,按照@Konrad Rudoplh建议< /a> 并为基类定义和使用复制构造函数。

You will have to store off the parameters when you first construct an object (probably in the superclass since that's what's using them) and then use them again on copy construction i.e. read them back out from the object you are copying from.

Edit

Better still, do as @Konrad Rudoplh suggests and define and use a copy constructor for the base class too.

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