c++ - 模板类和在 C++ 中使用带有混合参数的重载构造函数

标签 c++ templates

对于我的构造函数和重载构造函数定义,我有:

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/

相关文章:

c++ - 在 C++ 控制台应用程序中编写 sql 查询?

javascript - 覆盖未使用的 div

html - 使用 tsify (Typescript) 时,需要 HTML 文件作为 Browserify 中的模板字符串

c++ - 如何在 gdb 中打印长字符串的完整值?

c++ - 面对昂贵的交换,双枢轴快速排序

c++ - 成员模板的别名模板

c++ - boost::any 列表上的多态运算符

c++ - 如何简化执行模板函数的 switch 语句?

c++ - 当我初始化一个大小为变量的数组时会发生什么?

c++ - 具有引用计数的共享智能指针实现