tags:

views:

146

answers:

2

The fragment below is from a VC++ 2008 Express Edition. Say, I have a class with a member that is a struct. I am trying to define default values for the member variables of this class. Why this does not work?

struct Country{
    unsigned chart id;
    unsigned int initials;
    std::string name;
};

class world{
private:
    Country          _country;
    unsigned int  _population;
public:
    world(){};
    world():
             _country(): 
                 id('1'), initials(0), name("Spain") {};
             _population(543000) {}
    :
    :
    ~world(){};
};
+4  A: 

There are two ways to initialize the country member data. Like this ...

struct Country{
    unsigned char id;
    unsigned int initials;
    std::string name;
};

class world{
private:
    Country          _country;
public:
     world()
     {
         _country.id = '1';
         _country.initials = 0;
         _country.name = "Spain";
     }
     ~world(){};
};

... or, like this ...

struct Country{
    unsigned char   _id;
    unsigned int    _initials;
    std::string _name;
    Country(
        unsigned char id,
        unsigned int initials,
        const std::string& name
        )
        : _id(id)
        , _initials(initials)
        , _name(name)
    {}
};

class world{
private:
    Country          _country;
public:
    world()
    : _country('1', 0, "Spain")
    {
    }
    ~world(){};
};

Note that in the second example I find it easier to initialize the Country instance because I defined a constructor as a member of the Country struct.

Or, perhaps you want to give the Country type a default constructor:

struct Country{
    unsigned char   _id;
    unsigned int    _initials;
    std::string _name;
    Country()
        : _id('1')
        , _initials(0)
        , _name("Spain")
    {}
};


class world{
private:
    Country          _country;
public:
    world()
    {
    }
    ~world(){};
};
ChrisW
Or both: provide a default constructor and another that takes the whole set of values. In C++ structs are not limited to only data. +1
David Rodríguez - dribeas
There is no need to provide destructor or copy constructor as the default generated one suffices.
David Rodríguez - dribeas
+1  A: 

The structure is an aggregate type.

Since it has no constructor you cannot initialise it with normal brackets, you can however use curly braces as you would initialise an array.

Stinomus
Have you tested that? I would have guessed that you can use that syntax to initialize a Country instance when you define it ... whereas you're assigning it to an already-created Country instance, which I'm not sure is legal.
ChrisW
believe that you can write: country_ = { '1', 0, "Spain" } inside the constructor body (curly braces). That should create a temporary Country object initialized with the values and then assign it to country_. It is not efficient but should work.
David Rodríguez - dribeas
It seems to depend on the compiler. Explicitly creating a temporary and assigning it to a class-scope member after initialising it with the curly braces will work but doing it as you say - in theory the exact same thing - doesn't seem to work on all compilers.
Stinomus