views:

900

answers:

3

1.

I know that it is possible to initialise an array of structures in the declaration. For example:

struct BusStruct
{
    string registration_number;
    string route;
};

struct BusStruct BusDepot[] =
{
    { "ED3280",    "70" },
    { "ED3536",    "73" },
    { "FP6583",    "74A" },
};

If the structure is changed into a class, like this:

class BusClass
{
protected:
    string m_registration_number;
    string m_route;
public:
    // maybe some public functions to help initialisation
};

Is it possible to do the same as for the structure (i.e. declare and initialise an array of classes at the same time)?

2. Am I correct to think that it is not possible to declare and initialise vector<BusStruct> or vector<BusClass> at the same time?

+1  A: 
  1. No, you would not be able to initialize classes the way you can with structures. But you can write the class constructor inside the array declaration.
  2. C++ has no built in way of initializing vectors, unless you want to load the vector from an array that you have initialized.
CookieOfFortune
Thank you for your answer. It confirms my suspicions and I now need to find a way to initialise them.
Andy
+2  A: 

Is it possible to do the same as for the structure (i.e. declare and initialise an array of classes at the same time)?

Not unless you create a suitable constructor:

class BusClass
{
protected:
    string m_registration_number;
    string m_route;
public:
    // maybe some public functions to help initialisation
    // Indeed:
    BusClass(string const& registration_number, 
             string const& route)
    :m_registration_number(registration_number),
     m_route(route) { }
};

Or you make all members public and omit the constructor, in which case you can use the same initialization syntax as for the struct. But i think that's not what you intended.

Am I correct to think that it is not possible to declare and initialise vector<BusStruct> or vector<BusClass> at the same time?

No it's not possible with current C++. You can however use libraries that make this possible. I recommend Boost.Assign for that. For that, however, your class has to have a constructor, and likewise your struct too - Or you need to create some kind of factory function

BusStruct make_bus(string const& registration_number, 
                   string const& route) { ... }

If you want to keep the struct initializable with the brace enclosed initializer list in other cases.

Johannes Schaub - litb
Thank you for your explanation and for updating my original post (to make the bit behind vector viewable). I do not really mind whether it is public or protected, but your suggestion about the boost library is good. I will take a look.
Andy
A: 

C++ natively supports two forms of vector initialization and neither is what you are looking for.

1: Every element the same as in:

vector<int> ints(4,1000); //creates a vector of 4 ints, each value is 1000.

2: Copy from an existing vector as in:

vector<int> original(3,1000); //original vector has 3 values, all equal 1000.
vector<int> otherVector(original.begin(),original.end()); //otherVector has 3 values, copied from original vector
Alex B
You are right, but unfortunately I do not want to initiate all the elements with the same starting value!
Andy