这是类A
中定义的结构struct structure
。 B 类
嵌套在 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/