template<class, class>模板参数为两个类

Apollo  hdmap_common.h 有段代码看不懂专门写了一个类似的双类模板

template <class Object, class GeoObject>
class ObjectWithAABox {
 public:
  ObjectWithAABox(const apollo::common::math::AABox2d &aabox,
                  const Object *object, const GeoObject *geo_object,
                  const int id)
      : aabox_(aabox), object_(object), geo_object_(geo_object), id_(id) {}
  ~ObjectWithAABox() {}
  const apollo::common::math::AABox2d &aabox() const { return aabox_; }
  double DistanceTo(const apollo::common::math::Vec2d &point) const {
    return geo_object_->DistanceTo(point);
  }
  double DistanceSquareTo(const apollo::common::math::Vec2d &point) const {
    return geo_object_->DistanceSquareTo(point);
  }
  const Object *object() const { return object_; }
  const GeoObject *geo_object() const { return geo_object_; }
  int id() const { return id_; }

 private:
  apollo::common::math::AABox2d aabox_;
  const Object *object_;
  const GeoObject *geo_object_;
  int id_;
};

双类模板 

#include <iostream>
using namespace std;

class IntClass {

  public:
    void IntFunction() {
      cout << "IntFunction" << endl;
    };
};

class DoubleClass {

  public:
    void DoubleFunction() {
      cout << "DoubleFunction" << endl;
    };
};

template <class IntClass, class DoubleClass> 
class IntDouble {
  public:
      void test() {
        int_class.IntFunction();
        double_class.DoubleFunction();
      };
  private:
    IntClass int_class;
    DoubleClass double_class;

};

int main()
{
  IntDouble<IntClass,DoubleClass> IntDoubleTest;
  IntDoubleTest.test();

  return 0; 
}


结果:
IntFunction
DoubleFunction



 双类模板的基础上调用函数

#include <iostream>
#include <memory>
#include "library.h"
using namespace std;


//定义第一个类
class IntClass {
  public:
    void IntFunction() {
      cout << "IntFunction" << endl;
    };

};


//定义第二个类,有个传参函数
class DoubleClass {
  public:
    void DoubleFunction() {
      cout << "DoubleFunction" << endl;
    };
    double DistanceTest(double a) {
      double a_;
      a_ = a;
      return a_;
    };
};


//创建一个双类的模板
template <class IntClass, class DoubleClass> 
class IntDouble {
  public:
      void test() {
        int_class.IntFunction();
        double_class.DoubleFunction();
      };
      //IntDouble模板创建一个DistanceTest()的函数
      double DistanceTest(double a) {
      //double_class调用自己的函数DistanceTest(),
        return double_class->DistanceTest(a);
      };
  private:
    IntClass *int_class;
    DoubleClass *double_class;
};

int main()
{
  double u = 20.2;
  IntDouble<IntClass,DoubleClass> IntDoubleTest;
  double e = IntDoubleTest.DistanceTest(u);
  cout << "e: " << e << endl;

  return 0; 
}



结果:
e: 20.2

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值