C++ make a variable type depend on user input

前端 未结 4 1846
青春惊慌失措
青春惊慌失措 2021-01-18 02:41

I want a function that creates an array for testing purposes:

  • The idea es to make the user select the type of elements the array will contain (int, float, doub
4条回答
  •  天涯浪人
    2021-01-18 03:08

    Since C++ is a statically typed language, you cannot really do this in a simple way. You cannot determine types at runtime, these are fixed as soon your program has been compiled.

    The closest thing you can get for your case IMHO, is using something like a boost::variant or boost::any.

提交回复
热议问题