I have a question about syntax regarding to template of class.
A List in my program is a base class that can hold integers. My teacher told us that this class can also contain objects of another class, which may be a derived class of the class List, using template.
My code for the class List starts like this...
1 2 3 4 5 6
template <typename T>
class List{
...
}
//and its derived class Pets
In the code above, template T used to be integer, so the List was able to contain a bunch of integers.
Can someone please teach me about syntax for including Pets objects instead of integers in the class List using template?
I also want to make a function that can add one Pet object at a time to the List.
void addPets(unknown parameter) // He suggests that we have to use template here, but I don't know what should be parameter(s) of this function and its corresponding syntax.
#include <iostream>
#include <list>
struct pet
{
pet( const std::string& its_name ) : name(its_name) {}
pet( const std::string& its_name, const std::string& its_colour ) : name(its_name), colour(its_colour) {}
std::string name ;
std::string colour = "unspecified colour" ;
};
// My teacher told us that this class can also contain objects of another class,
// which may be a derived class of the class List, using template.
struct pet_list : std::list<pet> // substitute your home-grown list class for std::list
{
using std::list<pet>::list ;
// void addPets(unknown parameter) // He suggests that we have to use template here
template < typename... ARGS > void add_pet( const ARGS&... args )
{ std::list<pet>::push_back( pet(args...) ) ; }
};
int main ()
{
pet_list my_pets ;
my_pets.add_pet( "Fido", "charcoal" ) ;
my_pets.add_pet( "Felix" ) ;
my_pets.add_pet( "Mina", "green" ) ;
for( const pet& p : my_pets ) std::cout << p.name << " (" << p.colour << ")\n" ;
}
#include <iostream>
#include <list>
struct pet
{
pet( std::string its_name ) : name( std::move(its_name) ) {}
pet( std::string its_name, std::string its_colour )
: name( std::move(its_name) ), colour( std::move(its_colour) ) {}
std::string name ;
std::string colour = "unspecified colour" ;
};
// My teacher told us that this class can also contain objects of another class,
// which may be a derived class of the class List, using template.
struct pet_list : std::list<pet> // substitute your home-grown list class for std::list
{
using std::list<pet>::list ;
// void addPets(unknown parameter) // He suggests that we have to use template here
template < typename... ARGS > void add_pet( ARGS&&... args )
{ std::list<pet>::emplace_back( std::forward<ARGS>(args)... ) ; }
};
int main ()
{
pet_list my_pets ;
my_pets.add_pet( "Fido", "charcoal" ) ;
my_pets.add_pet( "Felix" ) ;
my_pets.add_pet( "Mina", "green" ) ;
for( const pet& p : my_pets ) std::cout << p.name << " (" << p.colour << ")\n" ;
}