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

How to initialize a pointer to a structure in C++?

本文关键字:结构 指针 初始化 C++      更新时间:2023-10-16

这是class Astruct 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 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;                
    };