如何在 C++ 中初始化指向结构的指针?
How to initialize a pointer to a structure in C++?
这里是class A
中定义的结构体struct structure
。 Class B
嵌套在 Class 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的默认构造函数中初始化指针。像
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;
};
这里是class A
中定义的结构体struct structure
。 Class B
嵌套在 Class 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的默认构造函数中初始化指针。像
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;
};