更新时间:2023-11-29 17:01:52
template< typename T>Box类
应该从非模板基继承(例如 BasicBox类
类).
template <typename T> class Box
should inherit from a non-template base (let's say class BasicBox
).
然后指向 BasicBox
的指针可以指向派生模板的专业化对象:
Then a pointer to BasicBox
can point to objects of the specializations of the derived template:
BasicBox *someBox = new Box<int>;
或者,因为在现代C ++™中应该避免手动管理内存,所以使用智能指针将是一个更好的主意:
Or, since in modern C++™ manually managing memory should be avoided, using a smart pointer would be a better idea:
std::unique_ptr<BasicBox> someBox = std::make_unique<Box<int>>();