gpt4 book ai didi

c++ - 你如何正确地初始化一个结构,它的成员是一个指向 const 值的 const 指针?

转载 作者:行者123 更新时间:2023-11-30 01:48:39 25 4
gpt4 key购买 nike

目前我正在做这样的事情:

struct foo
{
const int
*const a,
*const b,
*const c;

foo(int a, int b, int c)
: a(_a), b(_b), c(_c)
{
*_a = a;
*_b = b;
*_c = c;
}

private:
int _a[1], _b[1], _c[1];

};

但是有没有一种方法可以在不放入第二组指针(_a, _b, _c)的情况下做到这一点?

最佳答案

but is there a way to do this without putting in the second set of pointers (_a, _b, _c)?

当然。您可以使用:

foo(int a, int b, int c)
: a(new int(a)), b(new int(b)), c(new int(c)) {}

牢记The Rule of Three并为 foo 适本地实现复制构造函数、复制赋值运算符和析构函数。

关于c++ - 你如何正确地初始化一个结构,它的成员是一个指向 const 值的 const 指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30181950/

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