【cmu15445c++入门】(5)c++中的模板类

一、template模板类

除了模板方法【cmu15445c++入门】(4)c++中的模板方法

模板也可以用来实现类

二、代码

/**
 * @file templated_classes.cpp
 * @author Abigale Kim (abigalek)
 * @brief Tutorial code for templated classes.
 */

// Includes std::cout (printing).
#include <iostream>

// Templates can be also used to implement classes. For instance, here is a
// basic templated class that stores one element of a templated type and
// prints it when the print function is called.

//模板也可以用来实现类
template<typename T>
class Foo {
  public:
    Foo(T var) : var_(var) {}
    void print() {
      std::cout << var_ << std::endl;
    }
  private:
    T var_;
};

// You can also pass in multiple type names via templates into classes. 
// For instance, here's another basic templated class that stores two
// elements of a templated type and prints them when the print function
// is called.
// 模板类可以支持多个类型
template<typename T, typename U> 
class Foo2 {
  public:
    Foo2(T var1, U var2) 
      : var1_(var1)
      , var2_(var2) {}
    void print() {
      std::cout << var1_ << " and " << var2_ << std::endl;
    }
  private:
    T var1_;
    U var2_;
};

// It is also possible to create specialized templated classes, that do
// different things for different types. Take the following contrived example,
// which instantiates a class with a print function that outputs the value of
// the variable stored if it's any other type but float. If the class is 
// instantiated with a float type, it prints out hello float and the variable
// the class stores in its var_ field.

//模板类也可以针对特定的类型做特定的处理
template<typename T>
class FooSpecial {
  public:
    FooSpecial(T var) : var_(var) {}
    void print() {
      std::cout << var_ << std::endl;
    }
  private:
    T var_;
};

// Specialized templated class, specialized on the float type.
template<>
class FooSpecial<float> {
  public:
    FooSpecial(float var) : var_(var) {}
    void print() {
      std::cout << "hello float! " << var_ << std::endl;
    }
  private:
    float var_;
};

// Template parameters don't have to be types. They can also be values!
template<int T>
class Bar {
  public: 
    Bar() {}
    void print_int() {
      std::cout << "print int: " << T << std::endl;
    }
};

int main() {
  // First, let us construct an object from a templated class. The Foo
  // class template is instantiated with an int template argument. This
  // would make a's type class Foo<int> instead of Foo. a's print 
  // function works as expected.
  Foo<int> a(3);
  std::cout << "Calling print on Foo<int> a(3): ";
  a.print();

  // It is also possible for a templated class to interpret the type
  // of its arguments. Once again, if you're a beginner, think twice
  // before doing this if you are unsure of the types you are 
  // instantiating your class with.
  Foo b(3.4f);
  std::cout << "Calling print on Foo b(3.4f): ";
  b.print();

  // Second, we construct an object from a templated class with multiple
  // type arguments.
  Foo2<int, float> c(3, 3.2f);
  std::cout << "Calling print on Foo2<int, float> c(3, 3.2f): ";
  c.print();

  // Let's see what happens when we instantiate FooSpecial both with
  // and without the float type argument. As expected when we call
  // print from d, it prints the variable and not "hello float".
  // When we call print from e, which is an instance of the
  // instantiated FooSpecial<float> class, it prints hello float!
  FooSpecial<int> d(5);
  std::cout << "Calling print on FooSpecial<int> d(5): ";
  d.print();

  FooSpecial<float> e(4.5);
  std::cout << "Calling print on FooSpecial<float> e(4.5): ";
  e.print();

  // Lastly, let's see what happens when we construct an object from a
  // templated class with non-type arguments.
  Bar<150> f;
  std::cout << "Calling print_int on Bar<150> f: ";
  f.print_int();

  // Once again, these are contrived examples, but it is still important
  // to understand them you'll be seeing code similar to this in the Bustub
  // codebase, so it's good to understand templated classes in these contexts!
  // 最后,需要注意的是,以上大多数都是人为的示例,但还是要理解下,可能在海量的代码中看到。
  return 0;
}

三、运行结果

相关推荐

  1. C++入门

    2024-01-12 11:28:04       42 阅读
  2. C#

    2024-01-12 11:28:04       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-12 11:28:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-12 11:28:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-12 11:28:04       82 阅读
  4. Python语言-面向对象

    2024-01-12 11:28:04       91 阅读

热门阅读

  1. Spring Boot Starter介绍和实战

    2024-01-12 11:28:04       46 阅读
  2. Spring Boot Starter设计实现

    2024-01-12 11:28:04       40 阅读
  3. 2024最新面试经验分享

    2024-01-12 11:28:04       68 阅读
  4. 频数表和列联表,以及进一步处理分析 -- R

    2024-01-12 11:28:04       47 阅读
  5. Spring Boot各类变量的使用

    2024-01-12 11:28:04       55 阅读
  6. Android 车联网——CarInputService介绍(十七)

    2024-01-12 11:28:04       56 阅读
  7. [手写爬虫框架],从回忆Python到爬虫原理解析

    2024-01-12 11:28:04       46 阅读
  8. 【FLV】记录 H.264的解析

    2024-01-12 11:28:04       58 阅读