Thread: multiple dispatch

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    15

    multiple dispatch

    Hey everyone!

    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?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    15
    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...

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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?

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    15
    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).
    Code:
    string operator + (int num)
    {
    	stringstream out;
    
    	out << num;
    	_word = _word + out.str();
    	return _word;
    }

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    15
    You'r first diagnosis was accurate. I am using 3 deriven cllases name Int, Float and String. The operator was just for the example...

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    15
    That what it suppos to return.
    But that's the smallest part of my problem...
    The big thing is described above

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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:
    1. Int + Int
    2. Float + Float
    3. Int + Float
    4. Float + Int
    5. String + String
    6. Int + String
    7. String + Int
    8. Float + String
    9. 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++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 06:31 AM
  3. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM