简体   繁体   English

如何将不同的实例应用于同一对象的名称?

[英]How to apply different instantiations to the same name of object?

int lim;    
do{
      cin>>lim;
      switch(lim)
      {
      case 1: {mpa<1,int,const char*> C; }break;
      case 2: {mpa<2,int,const char*> C; }break;
      case 3: {mpa<3,int,const char*> C; }break;
      case 4: {mpa<4,int,const char*> C; }break;
      case 5: {mpa<5,int,const char*> C; }break;
      default: cout<<"Incorrect number, please repeat\n";
      }
 }while(lim<1 || lim>5);

I want to pass a value(1,2,3,4 or 5) to the template int a,typename T1,typename T2. 我想将值(1,2,3,4或5)传递给模板int a,typename T1,typename T2。 I need different instantiations of this template. 我需要这个模板的不同实例。 But object C will be destroyed after switch. 但是切换后对象C将被销毁。 How to apply different instantiations to the same name of object? 如何将不同的实例应用于同一对象的名称?

In this case, I think the best option is to rethink class mpa to not require that first parameter at compile time. 在这种情况下,我认为最好的选择是重新考虑类mpa以便在编译时不需要第一个参数。 Seriously, everyone hates rewriting things, but that's what you should do. 说真的,每个人都讨厌重写一些东西,但这就是你应该做的。

Since I know you'll ignore that, here's a workaround: 既然我知道你会忽略它,这是一个解决方法:

Make a class mpa_base<class, class> , that mpa inherits from, and has all the same functions, but they're all virtual (ESPECIALLY the destructor). 创建一个类mpa_base<class, class>mpa继承,并具有所有相同的功能,但它们都是虚拟的(特别是析构函数)。 Then, you can do this: 然后,你可以这样做:

typedef mpa_base<int, const char*> mpa_int_pchar;
std::unique_ptr<mpa_int_pchar> C; //this is a smart pointer to a mpa object
int lim;    
do{
      cin>>lim;
      switch(lim)
      {
      case 1: C.reset(new mpa<1,int,const char*>()); break;
      case 2: C.reset(new mpa<2,int,const char*>()); break;
      case 3: C.reset(new mpa<3,int,const char*>()); break;
      case 4: C.reset(new mpa<4,int,const char*>()); break;
      case 5: C.reset(new mpa<5,int,const char*>()); break;
      default: cout<<"Incorrect number, please repeat\n";
      }
 }while(lim<1 || lim>5);

Refactor the common part in another function and then you can easily do this. 重构另一个函数中的公共部分,然后您可以轻松地执行此操作。 Also, always have the input from the stream as part of the loop check or make it a break-condition. 此外,始终将流中的输入作为循环检查的一部分或使其成为中断条件。

template<class MPA>
void foo(MPA const& mpa){
  // whatever you want to do, do it here
}

int lim = 0; 
do{
  if(!(cin >> lim)){  // broken input
    cin.clear(); // clear error flags
    break; // bail out
  }
  switch(lim)
  {
  case 1: { foo(mpa<1, int, char const*>(/*params*/)); }break;
  case 2: { foo(mpa<2, int, char const*>(/*params*/)); }break;
  case 3: { foo(mpa<3, int, char const*>(/*params*/)); }break;
  case 4: { foo(mpa<4, int, char const*>(/*params*/)); }break;
  case 5: { foo(mpa<5, int, char const*>(/*params*/)); }break;
  default: cout<<"Incorrect number, please repeat\n";
  }
}while(lim < 1 || lim > 5);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM