I’m relative new to C++ and my background is in Java. I have to port some code from Java to C++ and some doubts came up relative to the Object Java’s class. So, if I want to port this:
void setInputParameter(String name, Object object) { ..... }
I believe I should use void* type or templates right? I don’t know what’s the “standard” procedure to accomplish it.
Thanks
It depends what you want to do with
object.If you use a template, then any methods you call on
objectwill be bound at compile time toobjects type. This is type safe, and preferable, as any invalid use of the object will be flagged as compiler errors.You could also pass a
void *and cast it to the desired type, assuming you have some way of knowing what it should be. This is more dangerous and more susceptible to bugs in your code. You can make it a little safer by usingdynamic_cast<>to enable run-time type checking.