tags:

views:

75

answers:

4

Hi all.

Can any one tell me how to declare constructor for a struct? My struct is delared 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 please tell me how to declare constructor for my struct Ship and where do I have to implement that constructor in .h file or in cpp file?

Your urgent help is highly needed.

A: 

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
}
JoshD
+1  A: 

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
}
AndreyT
+5  A: 

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; 
Steve Townsend
+1 -- the initializer list is almost certainly the right way to go.
Jerry Coffin
Nitpicking, but you can't really use `class` as an identifier, can you?
Pedro d'Aquino
@Pedro - good point - edited to avoid confusion for OP
Steve Townsend
A: 

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