在运行时选择main()中的模板类类型



所以我知道模板必须在编译时确定,但我想知道是否有一种方法可以在运行时高效地选择哪种模板对象类型,而不是为每种类型创建一个用例并复制粘贴大量只为分别处理每种类型而更改的代码。

我希望用户输入哪种类型(float、string、int(,然后为相应的类型创建一个对象BinTree,然后在一个单独的switch语句中使用它,该语句可以处理对象,而不管它的类型是

int main(int argc, char **argv) {
/***
*  Read argv for file code here
**/
bool validInput = true;
enum ValueTypes {aInt, aFloat, aString};
ValueTypes ValueType;
// somehow declare a pointer for the object BinTree<T> here so it is accessable throughout main.
while (validInput)
{
cout << "Enter list type (i - int, f - float, s - std:string):t" << endl;
char charInput;
printf("Enter a command: ");
cin >> charInput;
/**
* this switch will determine the type and instantiate a bintree object respective to it.
*
*/
switch (charInput)
{
case 'i':
{
printf("This is case in");
BinaryTree<int> BinTree;
validInput = false;
ValueType = aInt;
break;
}
case 'f':
{
printf("This is case fn");
BinaryTree<float> BinTree;
validInput = false;
ValueType = aFloat;
break
}
case 's':
{
printf("This is case sn");
BinaryTree<string> BinTree;
validInput = false;
ValueType = aString;
break;
}
default:
{
cin.clear();
cin.ignore();
cout << "Please enter a valid option." << endl;
break;
}
}
} // while ValidInput
validInput = true;
while (validInput)
{ // while
/**
* I am trying to get this while loop and switch statement to use binTree<T> regardless of
* T's type.
**/

string userPrompt2 = "these are user commands such as input 'i' to insert item";
cout << userPrompt2;
string stringInput;
float floatInput;
int intInput;
char charInput;
cin >> charInput;
switch (charInput)
{
case 'i':
{
/**
*  These if cases are to handle different types from user input for cin <<
* 
*/
cout << "Item to insert: ";
if (ValueType == aString)
{
cin >> stringInput;
BinTree.Insert(stringInput);
}
else if (ValueType == aFloat)
{
cin >> floatInput
BinTree.Insert(floatInput);
}
else if (ValueType == aInt)
{
cin >> intInput;
BinaryTree.Insert(intInput);

}
}
case 'd':
{
}
case 'r':
{
}
}
} // while

}

创建一个抽象基类,以独立于类型的方式提供您希望可用的所有操作,使用模板类来实现操作,并根据类型的选择动态分配此类型的对象。

下面是一个类的简化示例,该类读取类型为std::stringint的输入并将输入打印到stdout。您可以要求对象包装BinaryTree并提供类似的insert等。

// abstract wrapper type providing functionality for reading input & printing
struct BaseData
{
virtual ~BaseData() = default;
virtual void Read(std::istream& stream) = 0;
virtual void Print(std::ostream& stream) const = 0;
};
// concrete implementation for a specific type
template<class ValueType>
struct Data : BaseData
{
void Read(std::istream& stream) override
{
stream >> data;
}
void Print(std::ostream& stream) const override
{
stream << data;
}
ValueType data;
};
int main() {
std::unique_ptr<BaseData> m_data;
char type;
std::cin >> type;
// choose & create the wrapper type
switch (type)
{
case 'i':
m_data = std::make_unique<Data<int>>();
break;
case 's':
m_data = std::make_unique<Data<std::string>>();
break;
default:
std::cerr << "invalid typen";
return 1;
}
// use data-type independent operations
m_data->Read(std::cin);
m_data->Print(std::cout);
}

相关内容

  • 没有找到相关文章

最新更新