#include <iostream>
#include <memory>
#define REGISTER_MESSAGE_TYPE(data, type) \
data = std::make_shared<type>(); \
template<typename T>
class test
{
private:
std::shared_ptr<T> proto_;
public:
test() :proto_ (nullptr){
REGISTER_MESSAGE_TYPE(proto_,T);
}
void set(T v) {
*proto_ = v;
}
void print() const {
std::cout << *proto_<<std::endl;
}
~test() = default;
};
int main(int argc, const char** argv) {
test<int> t;
t.set(1);
t.print();
test<std::string> t1;
t1.set("aaaaa");
t1.print();
return 0;
}
模板实现一个任意类型创建
最新推荐文章于 2024-11-07 15:40:17 发布