1

Let's assume the type of some variable is given and the actual data is in some place in memory. Ex:

const type &type, const void *key

key is a pointer to some object in memory. "It can be any object of any data type". If I know the type of the given object, how do I create the actual object with the right data type?

The range of type is int,realint and char. I guess a switch statement is needed in this case; however, variables inside a switch just work for that scope. I need the variable for further usage.

edit: RawN interpretation is right. How to create a new variable at run time by knowing the type.

KingMetin2
  • 89
  • 1
  • 8
  • What exactly is `const type &`? What's an example of create one of those? And what is stored in `key`? What's an example of creating one of those? – John Zwinck May 21 '17 at 02:45
  • Is your question about [unions](http://en.cppreference.com/w/cpp/language/union)? –  May 21 '17 at 02:45
  • I will improve my question – KingMetin2 May 21 '17 at 02:47
  • 2
    Use either a union, or a `std::variant`. If you don't know what either means, keep reading your C++ book until you do. – Sam Varshavchik May 21 '17 at 02:49
  • How is code going to act on data if the type is unknown at compile time? – stark May 21 '17 at 02:51
  • If you want to cast the variable type at runtime, you'd use `dynamic_cast(variable);` – Abstract May 21 '17 at 02:51
  • 2
    @SamVarshavchik , your comment reminds me of this https://www.google.com/search?biw=1280&bih=542&tbm=isch&sa=1&q=new+coder+stack+overflow+memes&oq=new+coder+stack+overflow+memes&gs_l=img.3...1367.5923.0.5974.0.0.0.0.0.0.0.0..0.0....0...1.1.64.img..0.0.0.wfcTeFRh49Y#imgrc=OnmfEuWZGx5a7M: – KingMetin2 May 21 '17 at 02:53
  • 1
    Usually using a void pointer is a sign of a bad design – Ed Heal May 21 '17 at 02:54
  • C++ is strongly typed language, what you are trying to do is again its design. Most probably you either chose wrong language or wrong approach. This stuff can be done in C++ though but it is not a simple topic. Novice progarmmers very often try to do something like that, not realizing that it can be implemented much simpler way. – Slava May 21 '17 at 02:56
  • @Slava C++ strongly typed really http://stackoverflow.com/questions/26753483/is-c-considered-weakly-typed-why – PapaDiHatti May 21 '17 at 02:58
  • @Kapil read answer to that question, yes C++ is strongly typed. – Slava May 21 '17 at 02:59
  • I just try ... Does this help? http://coliru.stacked-crooked.com/a/a270f3ddb69b9567 – javaLover May 21 '17 at 03:04
  • @Slava I think the OP wants to deduce the type and then explicitly use the deduced type to declare a new variable. I am also interested to know how it is done. Without using Boost, that is. –  May 21 '17 at 03:27
  • @RawN I am not sure how you interpreted this question such way. – Slava May 21 '17 at 03:32
  • @Slava If we assume my interpretation was right, then what path to take after the `auto result = *pointer`? –  May 21 '17 at 03:35
  • @javaLover looks like ugly simulation of `std::variant`. That is already implemented by standard library, but it is not simple topic and you need to be experienced to use such constructs. When novice programmers want something like that they usually simply using wrong approach due to lack of experience. – Slava May 21 '17 at 03:35
  • @RawN do you mean `decltype(result) anotherVar;` ? Again I doubt that is what OP wants. – Slava May 21 '17 at 03:37
  • @Slava I can't agree more. You are right. To OP : more context is needed. – javaLover May 21 '17 at 03:37
  • @Slava Awesome. Thanks. I guess you are right. It is a bit unclear what the question is. –  May 21 '17 at 03:37
  • 1
    RawN interpretation is correct. – KingMetin2 May 21 '17 at 03:48

0 Answers0