我想获取构造函数的参数列表并保存其参数列表,然后传递此参数列表并使用“placement new”来实例化对象 .
这是我的核心代码:
#include <iostream>
class TestClass
{
public:
TestClass()
{
}
TestClass(int a, int b) {
this->a = a;
this->b = b;
}
int get()
{
return a + b;
}
public:
int a, b;
};
TestClass* testClass = new TestClass();
template<typename T>
void test(int a, int b)
{
// the arguments I need to pass in here are not fixedly written
// so I want to get a list of constructor arguments for the template class.
new (testClass)T(a, b);
}
int main()
{
test<TestClass>(1, 2);
std::cout << testClass->get() << std::endl;
system("pause");
return 0;
}