3

let's say I have the following :

void *pA;

now I want at runtime to convert this pointer to a type that is not known at compile time. i.e. what is the equivalent or how to emulate in ANSI C a c++ dynamic_cast ?

Thanks !

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • 7
    `dynamic_cast` doesn't allow you to cast to a type which is "not known at compile time", so what is the actual question here? You must know the type at compile time or you cannot perform the cast, period. – Ed S. Sep 07 '12 at 18:38
  • You can write a program that creates(or edits) *.C file then uses command-line to links it or compiles it then restarts itself – huseyin tugrul buyukisik Sep 07 '12 at 18:41
  • 2
    @tuğrulbüyükışık: I don't think that is a practical solution. OP, instead of asking us to solve the last 10% of your problem that makes no sense, please give us a high level description of the problem you are trying to solve with this. – Ed S. Sep 07 '12 at 18:43
  • IMHO - You should avoid casts at all costs. Usually (if not always?) it means that your software design is flawed. – Ed Heal Sep 07 '12 at 18:50
  • 5
    @EdHeal: That is ridiculous. How would you propose implementing a generic type in C? How would you propose implementing a callback function which takes a generic blob of data to pass back when the function is called? That statement is religious and just plain wrong. There exist numerous, completely valid reasons to perform a cast. – Ed S. Sep 07 '12 at 18:51
  • Oops - In C++ mode. In C++ mode the casts should be avoided. – Ed Heal Sep 07 '12 at 19:01
  • @EdHeal: Fair enough :). Though I would go with your "Usually..." version. – Ed S. Sep 07 '12 at 19:35
  • What's the point of casting to a type you don't know at compile time? What are you going to do after the casting? If you don't know the type in advance, you won't be able to do *anything* with it. – zmbq Sep 07 '12 at 23:02

2 Answers2

2

now I want at runtime to convert this pointer to a type that is not known at compile time. i.e. what is the equivalent or how to emulate in ANSI C a c++ dynamic_cast ?

Well, that's not what dynamic_cast does. You cannot cast to a type which is not known at compile time.

If you need to cast the object to a type which may change during the execution of your program depending on various conditions then it can be as simple as creating a switch statement which checks some variable (and enumerated value, whatever) to determine which cast to perform. It can then work with the known type.


On a side note, it would be advantageous for you to describe your high level problem instead of how to implement your proposed solution (which may or may not make sense). There may exist alternatives to your approach which will be better than what you have envisioned, but we can't offer any without knowing what problem you are taking a stab at solving.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
1

Something like this might work:

void get_val(void * buf, void *data, int type_id) {
  switch (type_id) {
    case INT: int *sp = (int*)data;
              int *dp = (int*)buf;
              *dp = *sp; 
              // or just *((int*)buf) = *((int*)data)
              break;
    case FLOAT:float *sp = (float*)data;
               float *dp = (float*)buf;
               *dp = *sp; 
               break;
   /* ... */
  }
}
perreal
  • 94,503
  • 21
  • 155
  • 181
  • 1
    There's no point in the casts, since `buf` and `data` are `void *` they convert without casts. – unwind Sep 07 '12 at 21:47
  • Unfortunately both `*sp` and `*dp` won't be accessible outside the `switch` block, which will make this solution a somewhat useless! – ilgaar Dec 21 '15 at 19:58
  • dp and sp are just available in the scope of the switch case statement. But what if I need them outside? – chris576 Feb 08 '21 at 10:15
  • you can introduce a structure with int_field, float_field kind of fields, or use union. this wasn't the main question I believe. – perreal Feb 08 '21 at 21:30