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
Related Q & A:
- How to construct a class diagram?Best solution by sourcemaking.com
- Is it correct to extend a bean class and write a class which holds logic to populate the bean we extend?Best solution by stackoverflow.com
- How do I call an objective-c method?Best solution by Stack Overflow
- Is the drainTo() method of a BlockingQueue atomic?Best solution by stackoverflow.com
- Where can I find a small end 4 pin fire wire and a 5 pin mini usb 2.0 cable that connects a external harddrive?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.