Search code examples
c++stlstdstdlist

C++ Is it possible to create a std::list of type T when a constructor is required for type T?


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


Solution

  • In C++11 you may use an initializer-list:

    #include <list>
    #include <string>
    
    int main() {
        // C++11 initializer-list
        std::list<std::string> species = { "empire", "macintosh" };
    
    
        // Without C++11: You may initialize with an array:
        const char* species_array[] = { "empire", "macintosh" };
        std::list<std::string> species_list(
            species_array,
            species_array + sizeof(species_array)/sizeof(species_array[0]));
        return 0;
    }
    

    With apples it is:

    int main() {
        // C++11 initializer-list
        std::list<apple> species = { apple("empire"), apple("macintosh") };
    
    
        // Without C++11: Initialize with an array:
        const apple species_arry[] = { apple("empire"), apple("macintosh") };
        std::list<apple> species_list(
            species_arry,
            species_arry + sizeof(species_arry)/sizeof(species_arry[0]));
        return 0;
    }