For example:
class apple
{
public:
string name;
apple::apple(string name) : name(name)
{
}
};
If I want to make a bunch of lists each with the type of apple, I thought I could do something like std::list<apple> empire("empire"), macintosh("macintosh")
. Basically I want to pass arguments for a constructor of class T declared by list<T>
when I'm creating a list. Sorry if I'm not explaining this right, feel free to edit my question if you have that ability.
Thanks
EDIT This question seems to be confusing and it's probably because I gave a bad example. I need to redesign my class. Following this example though what I wanted is a list that is all empire apples and each apple in that list has a designated type of empire, and a list that is all macintosh apples and each apple in that list has a designated type of macintosh.
So to clarify some or confuse some more here we go.
class apple
{
public:
string variety_name;
string description;
apple::apple(string variety_name, string description)
: variety_name(variety_name), description(description)
{
}
};
int _tmain(int argc, _TCHAR* argv[])
{
// Vlad from Moscow's answer
std::list<apple> empire(1, apple("empire", "picked yesterday")),
macintosh(1, apple( "macintosh", "picked yesterday"));
// Vaughn Cato's answer
empire.push_back(apple("empire", "picked today"));
macintosh.push_back(apple("macintosh", "picked today"));
for(list<apple>::iterator it=empire.begin(); it != empire.end(); ++it)
{
cout << it->variety_name << " " << it->description << endl;
}
for(list<apple>::iterator it=macintosh.begin(); it != macintosh.end(); ++it)
{
cout << it->variety_name << " " << it->description << endl;
}
return 0;
}
So as you can see it would be easier to store the variety once rather than each time; my class obviously needs a redesign but that doesn't make the answers any less valid. Everyone thanks for your help
In C++11 you may use an initializer-list:
With apples it is: