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

时间:2015-06-26 09:34:50

标签: c++ pointers initialization

Here is a defined structure struct structure in a class A. Class B is nested in 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;                
};

Now I want to initialize the pointer pStruct before I use it.
For example, I use pStruct->Output.push_back().

1 个答案:

答案 0 :(得分:0)

For example you can initialize the pointer in the default constructor of the class. Something like

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;                
    };