I need to create a factory function to create an instance that can accept any type with different number of arguments.
I’m having two classes Employee and contact and to create an instance for both classes by single factory function
I have no clue how to do that. help me to do that
class Employee
{
std::string m_name;
int m_id;
int m_salary{};
public:
template<typename T1, typename T2>
Employee(T1&& name, T2&& id, T2&& salary)
: m_name{ std::forward<T1>(name) }, m_id{ id }, m_salary{ salary }
{
std::cout << "template Employee constructor" << std::endl;
}
};
class Contact
{
std::string m_name;
long long int m_number;
std::string m_address;
std::string m_mail;
public:
template<typename T1, typename T2>
Contact(T1&& name, T2&& num, T1&& addr, T1&& mail)
:m_name{ std::forward<T1>(name) }, m_number{ num }, m_address{ std::forward<T1>(addr) }, m_mail{ std::forward<T2>(mail) }
{
std::cout << "template Contact constructor" << std::endl;
}
};
template<typename T1, typename T2>
Employee* createObject(T1&& a, T2&& b, T2&& c)
{
return new Employee{ std::forward<T1>(a), b, c };
}
I had tried a function for creating an instance for employee class. But i dont know how to do for both the classes
New contributor
Muthuraj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.