What is the difference between method and function in Scala?

C++ method for a class that works on 2 instances of that class?

  • This should be simple, but I just can't find the answer in books. I have a Class called Clock which stores the time. Once I have defined two Clock objects with different times in them, there is supposed to be a method in the Class which will determine the difference in time between the two.. so it has to accept obects of type 'Clock'. I'm confused because calling a method in a class requires it to be called using an object of that class (ie. clock1.timeDiff(Clock, Clock) ) . So should I make a 3rd Clock object ('clock1' above) just so the method is attached to something? I think there's a better way somehow.. Also.. if anyone knows how to have the call to the function 'mimic' the format 'clock1 - clock2', ie 'a - b', rather than 'clock.method(...,..)', I also can't find anything on this either. Thanks

  • Answer:

    You would need to use something called operator overloading. This is basically changing the default behavior of symbols like "+" or "-" to work with your objects by writing methods for them. Remember that you can use + or - with integers, double, float etc, but not with char* or any objects? This will allow you to define the behavior of +, -, +=, -=, *=, *, / etc. for an object. If done right, you can have a statement like this in your code: time_in_seconds = clockObject1 - clockObject2; Note that time_in_seconds does not have to be a third Clock object. It can be a double or float or whatever you want it to be. Here is a good article about how to do it: http://www.devarticles.com/c/a/Cplusplus/Operator-Overloading-in-C-plus/

stratguy... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.