I'm trying to deal multimethods issue with no success until now. I'll describe my problem shortly:
I have an abstract base class name Data, which contain one virtual abstract method - "getValue".
Now, 3 classes: A, B and C inherit from Data, while A handle an int, B handle a float and C handle a string.
I want the "getValue" method to rerutn int/float/string acordding to it's object type.
What do I do?
You can't do that, and it has nothing to do with multimethods. All runtime-dispatched calls, be they multimethods or simple virtual functions, must have compatible signatures. Functions with different return types are definitely not compatible. Type safety is a static property of a C++ program, not a dynamic one.
What you can do is make getValue return some mixture type, like a boost::variant or a boost::any. However, that just substitutes one problem for another, really. Perhaps it's an easier one to solve, though.
1st of all thanks for the reply...
Anyway i might not reconizing my problem / path to solution accurately .
I need to build a program that will allow the user to add float/int/string with each other (adding string and int/float is defined chaining them).
So i tought i'll build an abstract-base class, as described in my previous thread, and let 3 classed name Int, Float and String inherit from Data.
If that's not the solution, have you got any other idea ? (there is one tip saying the solution is got something to do with multiple dispatch)
It should be very simple for the user to even type: (A+B).print() while A is a String object containing the string "good", and B is an Int object containing the number 4.
The above code sould print "good4" on the screen...
Perhaps instead of returning the values in a GetValue function, you can functions that work with the classes themselves to handle the data properly so that the type of the "value" is never exposed to users of the abstract base class. Whether those functions are virtual in the base class or are non-member functions or are member functions of another class depends on the design and what needs to be done.
Edit: Didn't see your post when I posted.
What happens when you add a String and an Int (but before you call print())? Does it create a String object?
I'm afride i did not understand you explanation . Would you clarify it?
About the question you asked, adding string and int is possible due to + operaor (see bottom) I loaded on the String class (tough I haven't succeed, yet, to load the same operator on int/float classes. so for now i can only add string+number, and can not add number+string).
string operator + (int num)
out << num;
_word = _word + out.str();
Sorry, my explanation was written before your second post... you can ignore it.
Make the operator+ a non-member function and you should be able to add String and Int.
Your example uses string and int, but I thought you had derived classes called String and Float and Int? That is an important distinction because int is a built-in type and you cannot change the behavior of operator+ for it.
You'r first diagnosis was accurate. I am using 3 deriven cllases name Int, Float and String. The operator was just for the example...
So what would Int + String return? An Int or a String? Normally, if String + Int returns a String, then Int + String should also return a String.
That what it suppos to return.
But that's the smallest part of my problem...
The big thing is described above
Which thing described above? CornedBee answered your original question.
The part about gettin g(A + B).print() to show "good4" on the screen is what I'm talking about. How you do that depends on how you expect operator+ to act.
What are the types of the return values of the following equations:
- Int + Int
- Float + Float
- Int + Float
- Float + Int
- String + String
- Int + String
- String + Int
- Float + String
- String + Float
If they are different, then I can see where you might use double dispatch, I'm just trying to clarify the problem.
Did you find any good information about multiple dispatch or double dispatch through searching the web? There should be some good examples, hopefully in C++.