C ++替代模板化数据成员

时间:2017-07-26 12:55:23

标签: c++ templates design-patterns containers c++03

如果我有课程DataManager

class DataManager
{
 public:
   int getRecordInt( size_t i ) const;
   std::string getRecordString( size_t i ) const;

 private:
   std::vector<int> _vInt;
   std::vector<std::string> _vString;
}

我可以使用例如

访问记录
DataManager DM;
// .. populate object ...
int iValue = DM.getRecordInt(3);

在我的实际应用程序中,我将有超过一百种数据类型(除了int和std :: string),所以我想避免为每种类型编写一个单独的getter。

现在,如果C ++支持模板化变量名称(它没有),我可以将类实现为:

class DataManager
{
 public:
   template<typename T>
   T getRecord( size_t i ) const
   {
      return _v<T>[i];
   }

 private:
   std::vector<int> _v<int>;
   std::vector<std::string> _v<std::string>;
}

我有什么办法可以达到C ++的目的吗?

(请注意,尽管我已将示例简化为最低限度,但我的现实世界问题要复杂得多,并要求存在此类DataManager类。)

4 个答案:

答案 0 :(得分:4)

C ++ 14解决方案:

您可以做的一件事是将所有不同的向量放入std::tuple。然后,您可以使用std::get指定从元组中获取哪个向量,然后访问该向量。那看起来像是

class DataManager
{
 public:
   template<typename T>
   T getRecord( size_t i ) const
   {
      return std::get<std::vector<T>>(_v)[i];
   }

 private:
   std::tuple<std::vector<int>, std::vector<std::string>> _v{{1,2,3},{"a","b","c"}};
};

int main()
{
    DataManager d;
    std::cout << d.getRecord<std::string>(2);
}

哪个输出

c

Live Example

答案 1 :(得分:2)

如果您的类型只出现一次,则可以使用std::tuplestd::get

class DataManager
{
public:
   template<typename T>
   T getRecord( size_t i ) const
   {
      return std::get<std::vector<T>>(v)[i];
   }

 private:
   std::tuple<std::vector<int>, std::vector<std::string>> v;
}

答案 2 :(得分:2)

您不能拥有模板化变量,但您可以使用私有模板化方法返回相应的变量:

class DataManager
{
 public:
   template<typename T>
   T getRecord( size_t i ) const
   {
      return _v<T>()[i];
   }

 private:
   std::vector<int> _vInt;
   std::vector<std::string> _vString;
   template<typename T> std::vector<T>& _v() {
       throw std::logic_error("non managed type");
   }
};

template <> std::vector<int>& DataManager::_v<int>() {
   return _vInt;
}
template <> std::vector<std::string>& DataManager::_v<std::string>() {
   return _vString;
}

由您来决定它是否是您真实用例的可接受解决方案。

答案 3 :(得分:0)

如果Serge Ballesta's answer没有漂浮你的船(恕我直言,这是正确的方法),那么你可以咬紧牙关写一个宏(C ++ 03解决方案):

public class MainActivity extends AppCompatActivity {

    Button startGame;
    TextView questionTextView;
    questions question = new questions();
    ArrayList<String> answers; //gets the correct
    int correctAnswer;

    public void START (View view) {

        startGame.setVisibility(View.INVISIBLE);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Random rand = new Random();
        startGame = (Button) findViewById(R.id.startButton);
        questionTextView = (TextView) findViewById(R.id.questionTextview);
        questionTextView.setText(question.getQuestion());


        correctAnswer = rand.nextInt(4);

        for(int i = 0; i < 4; i++ ) {

            if(i == correctAnswer) {


            } else {


            }
        }
    }
}






public class questions {

    public String [] mquestions = {


            "Who is the 1st president of the United States?",
            "Who is the 2nd president of the United States?",
            "Who is the 3rd president of the United States?",
            "Who is the 4th president of the United States?",
            "Who is the 5th president of the United States?",
            "Who is the 6th president of the United States?",
            "Who is the 7th president of the United States?",
            "Who is the 8th president of the United States?",
            "Who is the 9th president of the United States?",
            "Who is the 10th president of the United States?",
            "Who is the 11th president of the United States?",
            "Who is the 12th president of the United States?",
            "Who is the 13th president of the United States?",
            "Who is the 14th president of the United States?",
            "Who is the 15th president of the United States?",
            "Who is the 16th president of the United States?",
            "Who is the 17th president of the United States?",
            "Who is the 18th president of the United States?",
            "Who is the 19th president of the United States?",
            "Who is the 20th president of the United States?",
            "Who is the 21st president of the United States?",
            "Who is the 22nd president of the United States?",
            "Who is the 23rd president of the United States?",
            "Who is the 24th president of the United States?",
            "Who is the 25th president of the United States?",
            "Who is the 26th president of the United States?",
            "Who is the 27th president of the United States?",
            "Who is the 28th president of the United States?",
            "Who is the 29th president of the United States?",
            "Who is the 30th president of the United States?",
            "Who is the 31st president of the United States?",
            "Who is the 32nd president of the United States?",
            "Who is the 33rd president of the United States?",
            "Who is the 34th president of the United States?",
            "Who is the 35th president of the United States?",
            "Who is the 36th president of the United States?",
            "Who is the 37th president of the United States?",
            "Who is the 38th president of the United States?",
            "Who is the 39th president of the United States?",
            "Who is the 40th president of the United States?",
            "Who is the 41st president of the United States?",
            "Who is the 42nd president of the United States?",
            "Who is the 43rd president of the United States?",
            "Who is the 44th president of the United States?",
            "Who is the 45th president of the United States?",

    };


    public String getQuestion() {

        String question = "";

        Random rand = new Random();
        //This randomizes the questions!!
        int randomNumber = rand.nextInt(mquestions.length);

        question = mquestions[randomNumber];

        return question;

    }


}

Demo