C++ 哪些成员是从模板参数继承的
如果我们有一个类模板,比如C++ 哪些成员是从模板参数继承的,c++,inheritance,class-template,C++,Inheritance,Class Template,如果我们有一个类模板,比如 template<class Type> class Field { .... }; 因此,哪些成员函数是从vector继承到myvelocityField模板参数的,当编译器找到具体类型的声明时,允许编译器执行泛型模板参数本身的替换。你没有继承任何东西 如果在字段类中的某个地方使用类型,则会相应地执行替换,编译器只会查找引用的方法 template<class Type> class Field { void Foo() {
template<class Type>
class Field {
....
};
因此,哪些成员函数是从
vector
继承到myvelocityField
模板参数的,当编译器找到具体类型的声明时,允许编译器执行泛型模板参数本身的替换。你没有继承任何东西
如果在字段
类中的某个地方使用类型
,则会相应地执行替换,编译器只会查找引用的方法
template<class Type>
class Field {
void Foo()
{
Type instanceOfType;
instanceOfType.clear();
}
void NeverCalledMethod()
{
Bar(); //bar doesn't exist
}
};
Field<vector> aField; // here the type Field<vector> is instantiated for the first time.
aField.Foo(); // only here the template class Foo() method is included by the compiler.
模板
类字段{
void Foo()
{
类型instanceOfType;
instanceOfType.clear();
}
void从不调用方法()
{
Bar();//Bar不存在
}
};
外地;//这里,类型字段是第一次实例化的。
aField.Foo();//只有在这里,编译器才包含模板类Foo()方法。
在某些情况下(例如,Bar()
的主体具有有效语法),如果从未调用它,编译不会受到主体中错误的影响。
由于
Bar()
从未调用它,而且编译器可以解析它,但不会尝试实际编译它,因此上述代码不会产生任何编译器错误 什么是向量?你的意思是std::vector
?没有任何东西是继承的。如果您想要继承,您必须编写类似模板类字段:public Type{}然后应用常规继承规则。Re。最后一句话,有一些限制。首先,它不能包含语法错误。
template<class Type>
class Field {
void Foo()
{
Type instanceOfType;
instanceOfType.clear();
}
void NeverCalledMethod()
{
Bar(); //bar doesn't exist
}
};
Field<vector> aField; // here the type Field<vector> is instantiated for the first time.
aField.Foo(); // only here the template class Foo() method is included by the compiler.