1. Home
  2. Computing & Technology
  3. C / C++ / C#
photo of David Bolton
David's C / C++ / C# Blog

By David Bolton, About.com Guide to C / C++ / C#

Answer to C++ Programming Puzzle

Wednesday July 9, 2008
The answer is there are at least four. The first one is quite subtle:
  1. You can overwrite the private variable like this!
    *Earth.GetPlanetName() = 0;
    That's not exactly private!
  2. It doesn't trap a null name.
  3. It leaks in two places! When the object goes out of scope, nothing frees up PlanetName. There's no destructor.
  4. When a new name is assigned, the old one is lost but the memory is not freed up. Another leak!
C++ is probably one of the most annoying languages for having subtle bugs - but I suppose that's the price for all that programming power. They catch me out as well! Mind you if you think that's bad try templates!
Comments
July 10, 2008 at 4:08 am
(1) Gags says:

This is really nice catch…
I was a C++ programmer.. now i really lost all of my touch with beautiful c++… this might be a good call for me to come home… !!

August 3, 2008 at 2:06 am
(2) Reextcox says:

I agreed with you

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#

©2009 About.com, a part of The New York Times Company.

All rights reserved.