Abdul Samad
Abdul Samad

Reputation: 5918

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?

Upvotes: 2

Views: 9638

Answers (4)

Steve Townsend
Steve Townsend

Reputation: 54128

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; 

Upvotes: 9

ChrisW
ChrisW

Reputation: 56083

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)
{
}

Upvotes: 0

AnT stands with Russia
AnT stands with Russia

Reputation: 320371

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
}

Upvotes: 1

JoshD
JoshD

Reputation: 12824

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
}

Upvotes: 0

Related Questions