c++ 中的 T 类(你的定义)
在 C++ 中使用类 T 的一个优点是可以减少在函数中重新定义数据类型的时间(如果这些数据类型是在其他函数中定义的,例如在 int main 中)。
template <class T>
void showabs(T number)
{
if (number < 0 )
number = -number;
cout << number << endl;
return 0;
}
int main()
{
int num1 = -4;
float num2 = -4.23f;
showabs(num1);
showabs(num2);
return 0;
}
所以在这种情况下,在没有类T的情况下,对于每种数据类型,我们都必须添加其相应的数据类型条件,即对于int,另一组if语句,对于float,另一组if语句。
我说得对吗?
The one advantage of using class T in c++ is to reduce the time to redefine data types in a function, if those data types are defined in other function, for example, in int main.
template <class T>
void showabs(T number)
{
if (number < 0 )
number = -number;
cout << number << endl;
return 0;
}
int main()
{
int num1 = -4;
float num2 = -4.23f;
showabs(num1);
showabs(num2);
return 0;
}
So in this case, without class T, for each data type, we have to add its corresponding data-type condition, that is, another set of if statement for int, and another one for float.
Am I correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它不一定是
T 类
。它可以是class Key
或您想要的任何内容(您也可以使用typename
代替class
)。正确的术语是函数模板。使用函数模板无需为可能传递给函数的每种类型重新定义函数。因此,如果您不使用模板,则必须定义两个函数:这意味着大量重复代码。在某些情况下,您可以使用
void*
指针(您在 C 语言中经常看到这种情况),但这样做会导致接口丑陋并且没有类型安全性。值得注意的是,实际上,您的两个函数调用
实际上指向两个不同的函数,一个接受浮点数,另一个接受整数。因此,模板允许编译器为我们进行复制和粘贴(以更智能的方式)。
It doesn't have to be
class T
. It could beclass Key
or whatever you want (You can also usetypename
in place ofclass
). The correct term would be function template. Using a function template removes the requirement to redefine a function for every type that may be passed to it. So if you didn't use templates you would have to define two functions:Which means a lot of repeated code. You can in some cases use
void*
pointers (You see this a lot in C), but doing so leads to an ugly interface as well as no type safety.It is worth noting that in reality, your two function calls
Actually point to two different functions, one that takes a float and one that takes an integer. So templates allow the compiler to do the copying and pasting for us (in a lot more intelligent way).
if
语句无助于声明number
参数的类型。我认为您的意思是您必须为要处理的每种类型重载 showabs 函数。如果是这样,那么是的,您走在正确的道路上。if
statements wouldn't help in declaring the type of thenumber
parameter. I think you meant that you would have to overload theshowabs
function for each type you want to handle. If so, then yes, you are on the right track.嗯,这不是“一个优势”,还有很多其他优势。模板是为了重用而设计的,因此您可以将模板化函数或类用于您编写时不知道的类型。
Well, it is not "the one advantage", there are many others. Templates are made for reusing, so you can use your templated functions or classes for types you did not know when you wrote them.
基本上,您是正确的,但是您可以使用模板做更多事情,即 template-metaprogramming< /a>.
为了澄清术语,
class T
也可以是typename Foo
。class T
是模板参数,带有模板参数的函数定义是函数模板,带有模板参数的类定义是类模板。Basically, you are correct, but there is a lot more you can do with templates, namely, template-metaprogramming.
To clarify on you terminology,
class T
could also betypename Foo
.class T
is a template parameter, a function-definition with a template parameter is a function-template and a class-definition with a template parameter is a class-template.