gpt4 book ai didi

c++ - 如何在 C++ 中初始化指向结构的指针?

转载 作者:太空宇宙 更新时间:2023-11-04 13:31:57 26 4
gpt4 key购买 nike

这是类A中定义的结构struct structureB 类 嵌套在 A 类 中。

class A
{
public:
struct structure // the structure I need
{
std::vector <std::vector <float>> Input;
std::vector <int> Output;
float ft;
B* bb;
};
private:
B b;
structure* pStruct;
};

现在我想在使用它之前初始化指针pStruct
例如,我使用 pStruct->Output.push_back()

最佳答案

例如,您可以在类的默认构造函数中初始化指针。有点像

class B {};

class A
{
public:
A() : b(), pStruct( new structure() ) { pStruct->bb = &b; }
struct structure // the structure I need
{
std::vector <std::vector <float>> Input;
std::vector <int> Output;
float ft;
B* bb;
};
private:
B b;
structure* pStruct;
};

关于c++ - 如何在 C++ 中初始化指向结构的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31069724/

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