last day (14 days later) » 

3:06 PM
0
Q: Send new type as paramter

user3165438The following method aimed to convert a pointer to a variable back to the original type. The method should get 2 parameters: pointer to variable and its type. void myMethod(void *pointer, ? myType) { //convert the pointer to its original type myType p = *((myType*) pointer); } ...

 
Avoid passing around void * in C++ if you can. This looks like an XY problem where you are actually trying to perform some form of multiple dispatch. Perhaps you should look into the Visitor Pattern, or the Acyclic Visitor Pattern.
 
@YoungJohn, The visitor pattern sounds interesting. Can you please provide an example that solves the issue? Thanks.
 
Honestly, I'm not 100% sure what exactly your issue is, so I cannot provide an example to solve it. You can find examples of the visitor pattern on wikipedia: en.wikipedia.org/wiki/Visitor_pattern It is used to perform multiple dispatch in languages that do not have other forms of multiple dispatch. The visitor pattern is likely to require a restructuring of your class hierarchy though.
 
@YoungJohn, Thank you. The goal is to make a virtual method to behave as a template method. Does the visitor pattern an appropriate solution?
@user2079303, I would like a virtual method to behave as a template method. The idea is to allow to send a generic type (void pointer) and to convert to back to the original one. Any ideas?
 
No, the visitor pattern would be used to dynamically select which behavior to perform for various combinations of types. Templates are used for static polymorphism (at compile time), virtual functions are used for dynamic polymorphism (at run time).
if all you want is to convert a type, then you should probably use a cast, such as dynamic_cast.
 
3:06 PM
@YoungJohn, Can I use it to convert void pointer to its original type? how?
 
with dynamic_cast you'd have to know the type you wanted to convert your pointer to: myType * p = dynamic_cast<myType>(pointer); if pointer cannot be cast to myType the resulting p would be a nullptr
I don't think this actually gets you any closer to what you're trying to do though, sorry.
 
@YoungJohn, This line is the method implementation. What should be the parameters?
Here I am
 
It is uncommon to try to pass a type as a parameter to a function. I think that is mostly where the misunderstanding for me is.
you would use `dynamic_cast` to perform a switch on various types, so something like

if(dynamic_cast<type1>(pointer))
{
func1();
}
else if(dynamic_cast<Type2>(pointer))
{
 func2();
}
You would use the Visitor Pattern to avoid the need to pass the type as a parameter.
 
I would like a method that gets any type of argument. The simple solution is a template method, but I cannot use it since the method is virtual. The
The idea, therefore, is to send void pointer
Then to get its content.
In order to get the content of pointer I have to cast it back.
Do you understand the goal?
Does the visitor pattern have a solution?
 
the problem with void pointer is that it doesn't provide you any information about what type it used to be, and it is not easy to pass along that type information. The way to pass along that type information might involve templates, but types are not a passable object to a function in c++.
if you had a class hierarchy for the pointer you were passing, instead of a void pointer, then you might be able to get the information you are looking for using virtual functions.
 
3:19 PM
OK,
I am not locked on the void pointer idea
How should I implement the new suggestion?
What do you mean in " class hierarchy"?
 
there are a lot of possible things you might do, and it largely depends on exactly what data you are trying to send, but to implement a class hierarchy you would start with a base class with virtual methods:
 
I like to send string or int or double. An approach?
 
class Base
{
public:
virtual void DisplayData() = 0;
}

class Derived : public Base
{
virtual void DisplayData() { cout << data << endl; };
}
 
Thanks! And how can I send any type of argument to the method?
 
class DerivedInt : public Base
{
public:
DerivedInt(int value) { data = value;}
virtual void DisplayData() { cout << data << endl; }
private:
int data;
};

// DerivedInt could be used for int data, you could write a similar DerivedString
// for string data, etc.
// all could be used through a pointer to Base
Base * myInt = DerivedInt(5);
Base * myString = DerivedString("Hello");
myDisplayFunc(myInt);
myDisplayFunc(myString);
 
3:30 PM
Where data is declared?
 
void myDisplayFunc(Base * base)
{
base->DisplayData();
}
doing this will result in DerivedString's version of DisplayData to be called when myDisplayFunc is passed a DerivedString, and DerivedInt's version of DisplayData to be called when passed a DerivedInt.
 
And so on for each type that I like...
Right?
 
data is declared in each of the Derived classes, depending on what data they require. It is probably a private member in those classes, and it should probably be initialized in the constructor of those classes.
 
I have to try it first. If this will success I'll notify you to post it as answer and accept it.
 
This is basically the essence of dynamic polymorphism in C++
 
3:33 PM
Thanks a lot for the time and effort!
 
I hope this works out for you.
 

last day (14 days later) »