简体   繁体   中英

Defining constructor for struct declared inside a class in C++

How can I declare constructor for a struct? My struct is declared in the private part of a class and I want to declare my constructor for it.

Below is my code

class Datastructure {

private:

        struct Ship
        {
            std::string s_class;
            std::string name;
            unsigned int length;

        } minShip, maxShip; 

        std::vector<Ship> shipVector;
public:

    Datastructure();
    ~Datastructure();
};

This my header file; how can I declare constructor for my struct Ship and where do I have to implement that constructor in .h file or in cpp file?

Constructor declared in header file

struct Ship
{
    Ship();
    std::string s_class;
    std::string name;
    unsigned int length;

    } minShip, maxShip; 

and implemented in code:

DataStructure::Ship::Ship()
{
  // build the ship here
}

or more likely:

DataStructure::Ship::Ship(const string& shipClass, const string& name, 
                          const unsigned int len) :
s_class(shipClass), name(_name), length(len)
{
}

with this in the header:

    struct Ship
    {
private:
        Ship();
public:
        Ship(const string& shipClass, const string& name, unsigned len);
        std::string s_class;
        std::string name;
        unsigned int length;

        } minShip, maxShip; 

You declare it the same way you declare any other constrctor

class Datastructure {
private:
  struct Ship
  {
    std::string s_class;
    std::string name;
    unsigned int length;

    Ship(); // <- here it is

  } minShip, maxShip; 

  std::vector<Ship> shipVector;
public:
  Datastructure();
  ~Datastructure();
};

And you define it the same way you define any other constructor. If it is inline, you define it in the header file. If it is not inline, you define it in implementation file

Datastructure::Ship::Ship()
{
  // whatever
}

Declare it right in your code there:

class Datastructure {

private:

    struct Ship
    {
        // Constructor!!!
        Ship();
        std::string s_class;
        std::string name;
        unsigned int length;

        } minShip, maxShip; 

    std::vector<Ship> shipVector;
public:

Datastructure();
~Datastructure();
};

Then to define, use the proper scope:

Datastructure::Ship::Ship()
{
   // stuff
}

Declare it in the struct:

class Datastructure { 

    struct Ship 
    {
        //Ship constructor declared
        Ship();

        ...etc...
    }
};

You can define its implementation inline, in the *.h file:

class Datastructure { 

    struct Ship 
    {
        //Ship constructor declared and defined
        Ship()
        : length(0)
        {
        }

        ...etc...
    }
};

Or you can define it in the *.cpp file:

//Ship constructor defined
Datastructure::Ship::Ship()
: length(0)
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM