【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;
}

三、运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

康雨城

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值