c++ 中的 T 类(你的定义)

发布于 2024-08-26 20:26:09 字数 459 浏览 6 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝眼泪 2024-09-02 20:26:09

它不一定是T 类。它可以是 class Key 或您想要的任何内容(您也可以使用 typename 代替 class)。正确的术语是函数模板。使用函数模板无需为可能传递给函数的每种类型重新定义函数。因此,如果您不使用模板,则必须定义两个函数:

void showabs(float number){
  if (number < 0 ) number  = -number;
  cout << number << endl;
}

void showabs(int number){
  if (number < 0 ) number  = -number;
  cout << number << endl;
}

这意味着大量重复代码。在某些情况下,您可以使用 void* 指针(您在 C 语言中经常看到这种情况),但这样做会导致接口丑陋并且没有类型安全性。

值得注意的是,实际上,您的两个函数调用

showabs(num1); //showabs<int>(num1);
showabs(num2); //showabs<float>(num2);

实际上指向两个不同的函数,一个接受浮点数,另一个接受整数。因此,模板允许编译器为我们进行复制和粘贴(以更智能的方式)。

It doesn't have to be class T. It could be class Key or whatever you want (You can also use typename in place of class). 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:

void showabs(float number){
  if (number < 0 ) number  = -number;
  cout << number << endl;
}

void showabs(int number){
  if (number < 0 ) number  = -number;
  cout << number << endl;
}

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

showabs(num1); //showabs<int>(num1);
showabs(num2); //showabs<float>(num2);

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).

许你一世情深 2024-09-02 20:26:09

if 语句无助于声明 number 参数的类型。我认为您的意思是您必须为要处理的每种类型重载 showabs 函数。如果是这样,那么是的,您走在正确的道路上。

if statements wouldn't help in declaring the type of the number parameter. I think you meant that you would have to overload the showabs function for each type you want to handle. If so, then yes, you are on the right track.

情丝乱 2024-09-02 20:26:09

嗯,这不是“一个优势”,还有很多其他优势。模板是为了重用而设计的,因此您可以将模板化函数或类用于您编写时不知道的类型。

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.

柏林苍穹下 2024-09-02 20:26:09

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 be typename 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文