Cool C++ Stuff
I was working on my Greek Engine today and ran across a problem that I didn't know the answer to. I had a base class: instrument, and on this base class I had a virtual method: getBestPrice(). This method looked at the last trade price and last quote price for the instrument, and based on that, returned the very best price for the instrument. It's pretty standard stuff.
I also had a derived class: option, based on instrument, and it had an overridden version of the getBestPrice() method. The problem was, I was in a situation where I wanted to always use the instrument version of the method - even when I had an instance of option. How do I do it?
Well… I knew that if I was in a method on option, I could simple say:
double option::doThis() { return instrument::doThis(); }
and it would run the super class' version. But that's not what I had. I had a pointer to an instrument instance - or maybe it was an option instance. Who knows? But the instrument method had to be run.
My co-worker googled this and found that the correct syntax for this is quite simple:
instrument *inst = NULL; ... double price = inst->instrument::getBestPrice();
and even if inst points to an instance of option, we'll be executing the instrument's version of the method. Sweet!
I'll be checking it on Monday. Pretty nice thing to remember.