Late binding, run time casting and optional parameters - too clever for their own good.
Posted in Coding on May 26, 2010 at 5:26 pm
Bit of a techie one this, but how C++ handles its run time type casting is clever and useful but can be less than intuitive. This trap, I have fallen into more than once, is an obscure one you may like to ponder and save away for when it bites you. I certainly fixed the problem quicker the second time I encountered it!
class Dad {
virtual void method() { print(”In class dad”);}
virtual void otherMethod() { print(”Do something”);}
}
class Son::Dad { // class inheriting from dad
virtual void method() { print(”In class son”);} // this method is over ridden
}
main(){
class Son son;
class Dad *ptr;
ptr = &son;
ptr->method(); // even though type is Dad, this will print “In class son” because the late binding knows it’s really a “son”
ptr->otherMethod(); // this will print “Do something” because the late binding knows it’s really a “son” but son doesn’t have an otherMethod of its own
}
That’s the kind of things we programmers do a lot - not because we like print out the class names but because the classes we use are more useful - say class Employee and classes Engineer and Salesman that both inherit the basic methods (holiday(), pay() etc.) but do different things for other methods (work(), think() etc.).
Everything is nice until you need to change the code (that could be the definition of my job really!)
You need to add a value to “method” so you change the code to:
class Dad {
virtual void method(int N) { print(”In class dad with value %d”, N);} // add parameter
virtual void otherMethod() { print(”Do something”);}
}
// Son stays the same
main(){
class Son son;
class Dad *ptr;
ptr = &son;
ptr->method(99); // this now prints “In class dad with value 99″ because Son doesn’t have a method called method that takes a parameter so I must want the one in Dad.
}
That should be obvious(!), what has bitten me is when I’ve changed the code to make use of optional parameters:
class Dad {
virtual void method(int N=0) { print(”In class dad with value %d”, N);} // add optional (defaulted) parameter
virtual void otherMethod() { print(”Do something”);}
}
// Son stays the same
main(){
class Son son;
class Dad *ptr;
ptr = &son;
ptr->method(); // this now prints “In class dad with value 0″ !!!!!! No change to Son, no change to this line but the affect is different - ouch!
}
I didn’t write the compiler but what I figure is happening is that because the parameter is optional (defaulted) the code
ptr->method();
becomes
ptr->method(0);
And we are back to “Son doesn’t have a method called method that takes a parameter so I must want the one in Dad.”
Which just teaches us what we should have known already - sometimes you need to be very clever just to be clever.
Bought a PC!
Posted in Ope Source Software, the company, the web, e-commerce on May 25, 2010 at 3:44 pm
Wow! I’ve bought a PC! You may think that’s the kind of thing I do a lot but I’ve been working from home (on and off) for 20 something years so initially having a PC at home (albeit a work one) was more than most people had. That first unit was very much “mine” - it shared home and work duties.
In 2000 and a bit I went back into an office so bought a decent PC for home. Having trawled the net for the cheapest I ordered it only to have the company go bust the next day. Fortunately I got the cash back as it was a credit card order and I went for safety and bought from the company I was working for. However, it came pretty much bare bones and I had to install my own memory as well as software. That PC (a P3) with various disk (60G), OS (dual booting 98 & XP) memory upgrades (maxed out at 256M) has staggered on until now. The lack of memory being the real issue.
I’ve built PC’s out of work cast offs for various family members and ended up keeping a laptop ebayed for the in-laws but beyond their eyesight and co-ordination to use. Unfortunately a dodgy power connection on the laptop (battery was dead from day one) meant you could lose your work at any time and as it was also maxed out with 256M of memory the chances of doing anything before it died were minimal.
So I’ve bought a new laptop. New, from a shop, with software installed, legal paid for software and everything - not just some old bit of kit I’ve reformatted and put a collection of freeware and borrowings on. It took it out the box, turned it on and it worked - spooky.
And where did this ageing techie go for his hardware? Having considered another ebay purchase, having price checked the company’s staff discounts and special offers, having perused the many el cheapo sites I’ve squirreled away over the years my arcane knowledge took me to….
..Tesco.
Yes, my local supermarket had a good basic laptop for not much over £300 (£329 but double points traded up gives me effectively another £25 off that and you can get a £10 off on line purchases). http://direct.tesco.com/q/R.207-0318.aspx is not quite what I’d have chosen given days to pore over specs but it was close enough to both the price and performance I had sketched out in my head and easy to get hold of and to take back if it was DOA that it was a no brainer in the end.
If you’d have told me 25 years ago I’d buy a laptop along with the weekly shop….
Past its sell by?
Posted in the company, the web, Microsoft on May 18, 2010 at 8:31 am
http://www.itpro.co.uk/623338/microsoft-calls-ie6-spoiled-milk
Well that’s the only browser my company allows…
The law is an ass (in 140 characters or less)
Posted in In the news, media, Uncategorized on May 14, 2010 at 8:40 am
http://www.itpro.co.uk/623140/brit-fined-1-000-for-angry-airport-tweet
I mean what is that about?
It’s not “grossly offensive or of an indecent, obscene” is it?
And how can it be “or menacing character” if even the airport doesn’t it as a real threat?
Only a person with a sense of humour bypass could see it as other than an attempt at a joke and unlike shouting “I’ve got a bomb, ha-ha-ha” on a crowded plane it wasn’t going to cause any danger.
The problem with this kind of rubbish (apart from for the guy charged) is that it brings the law, the legal system and the whole country into disrespect. Maybe I can sue the judge for behaviour liable to cause a breach of the peace?
Pay for free music?
Posted in Open Source Software, the web, music, Blogs, e-commerce on May 11, 2010 at 10:28 am
Back at http://www.itpro.co.uk/blogs/davef/2010/01/08/free-songs/
I was enthusing about capturing streamed audio. Seems I’m not the only one to see this as a good way to “acquire” music.
Roxio have just spammed me with:
“Capture free, high-quality digital music from the Internet
There are millions of great music tracks available to play for free from Internet music sites, Creator 2010 makes it easy to capture1 hi-quality streaming Internet audio, to add to your music library, burn onto CD and DVD, and convert to enjoy on iPod or other music players.”
I’ll stick with free Audacity http://audacity.sourceforge.net/ but Roxio are here http://www.roxio.com/ if you want to spend money.
Strong and stable government
Posted in In the news on May 7, 2010 at 1:00 pm
All I am hearing is we need “Strong and stable government”, the implication usually being only a majority party can provide this. On the contrary, an effective coalition would be far more stable than the “Punch and Judy politics” that I heard so much of before the election. I can understand that it may not be as strong, if strong means being able to make decisions without giving them sufficient consideration or wide based support, however, that is all to the good.
It is time our politicians grew up, stopped assuming they are right and everyone else is wrong and started reaching compromise and consensus the way the rest of us have to operate.
Election Incompetences
Posted in In the news, the company, the web, Security on at 11:39 am
Well it’s a small and coincidental world but my company’s election for works council members finished yesterday and as I am yet to receive a ballot paper I have some fellow feeling with http://news.bbc.co.uk/1/hi/uk_politics/election_2010/england/8666457.stm
Mistakes organising a company ballot aren’t good but surely as a country we are capable of allowing our citizens (subjects?) a chance to use the vote they have?
It could all be done on line but would I trust it? The problem is computer cock-ups tend to be more major and anyway if it was a government IT scheme you can be sure that it would cost a fortune and wouldn’t be ready on time…
Tag cloud
Archives
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
Most commented posts
Highest Rated Blog Posts
- PC Advance Required (100%)
- No excuse - it's free to encrypt! (100%)
- Virtualization's Dark Side - or stating the obvious for beginners (100%)
- Tabs - I might change my mind? (100%)
- Which Linux do you drink? (100%)
- Sat Nag (100%)
- How to be a Guitar Hero (100%)
- Home working - a tale of freedom, loneliness and slippers (100%)
- Vertigo (100%)
- Is your back door open? (90%)


