对于我的构造函数和重载构造函数定义,我有:
template <class T> Student<T>::Student(){}
template <class T> Student<T>::Student(string sName, int sAge) {
m_name = sName;
m_age = sAge;
}
我对如何在重载构造函数中调用具有混合参数的构造函数感到困惑。 我的理解是,如果它们都是整数,我会做类似的事情:
Student <int> newStudent;
newStudent(10, 15);
最佳答案
Student<int>
是一种类型。 Student<int> newStudent;
创建该类型的变量,这意味着它正在构造该对象。由于不包含任何参数,因此使用无参数构造函数。
newStudent(10, 15)
正在尝试调用 ::operator()(int, int)
该类的成员,可以定义也可以不定义。
你可能想要:
Student<int> newStudent("Mary", 15);
... 创建 Student<int>
在变量中输入对象 newStudent
.
关于c++ - 模板类和在 C++ 中使用带有混合参数的重载构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41047161/