C ++类,具有不同类型的相同成员

时间:2014-01-09 19:39:50

标签: c++

是否可以自动生成这样的内容?我只想要特定成员一次并生成三个类(它们相似但是......)

class recipe{
public:
  int timeToPrepare;
  std::string name;
}

class recipeQuery{
public:
  baseQuery<int> timeToPrepare;
  baseQuery<std::string> name;
}


class recipeScheme{
public:
  schemeItem<int> timeToPrepare;
  schemeItem<std::string> name;
}

2 个答案:

答案 0 :(得分:5)

这样的东西?

template <class T1 = int, class T2 = std::string> class recipe {
    T1 timeToPrepare;
    T2 name;
};

typedef recipe< baseQuery<int>, baseQuery<std::string> > recipeQuery;

typedef recipe< schemeItem<int>, schemeItem<std::string> > recipeQuery;

答案 1 :(得分:0)

template<template<typename> class T>
class recipe{
public:
    T<int> timeToPrepare;
    T<std::string> name;
};

typedef recipe<baseQuery> recipeQuery;
typedef recipe<schemeItem> recipeScheme;