如何模板静态工厂方法?

时间:2017-08-21 01:25:55

标签: c++ templates

如何模板化以下代码? 首先,我有一本书作为基础:

class Book {
public:
    Book() {}
    ~Book() {}
}

然后是电脑书:

class ComputerBook: public Book {
public:
    static ComputerBook* create() {
        return new ComputerBook();
    } 
private:
    ComputerBook ():Book() {}
}

然后是电话簿:

class PhoneBook: public Book {
public:
    static PhoneBook* create() {
        return new PhoneBook();
    } 
private:
    PhoneBook():Book() {}
}

PhoneBook有两个遗产:

class PhoneBook1: public PhoneBook {
public:
    static PhoneBook1* create() {
        return new PhoneBook1();
    } 
private:
    PhoneBook1():PhoneBook() {}
}


class PhoneBook2: public PhoneBook {
public:
    static PhoneBook2* create() {
        return new PhoneBook2();
    } 
private:
    PhoneBook2():PhoneBook() {}
}

那么可以将ComputerBook和PhoneBook1,PhoneBook2合并为一个带模板吗?

2 个答案:

答案 0 :(得分:2)

从您尝试做的事情来看,您正在为所有图书制作一个名为create的静态工厂方法。您可以像这样模板化这个方法:

class Book {
public:
    Book() {}
    ~Book() {}
    template<typename T>
    static Book* create() {
        return new T();
    }
}

然后制作电话簿:

Book::create<PhoneBook1>();

还要确保每本书的构造函数都是公共的,或Book::create静态方法的朋友。

答案 1 :(得分:1)

你想要的是(可能)CRTP。您可以在基类中定义方法create,使用派生类(模板参数)对其进行参数化。从模板库继承时,插入派生类并神奇地获取具有正确类型的create函数。

我不明白为什么你有私人构造函数或为什么你需要这个工厂函数。

template < typename Derived >
class Book {
  friend Derived;
public:
  Book() {}
  ~Book() {}
  static Derived* create() {
    return new Derived{};
  }
};

class ComputerBook: public Book<ComputerBook> {
  // Make the base class a friend so we can access the private constructor
  friend class Book<ComputerBook>;
private:
  ComputerBook() : Book() {}
};

template < typename Derived >
class PhoneBook: public Book<Derived> {}; // no private constructor, no 'friend' needed

class PhoneBook1: public PhoneBook<PhoneBook1> {};
class PhoneBook2: public PhoneBook<PhoneBook2> {};

int main()
{
  auto cb = ComputerBook::create();
  auto pb1 = PhoneBook1::create();
  auto pb2 = PhoneBook2::create();
  delete cb;
  delete pb1;
  delete pb2;
}