public, protected, private

In the first company I worked at I came across a bug that took me a while to figure out. At first I break-pointed the code where a value was changed, but that break-point was never hit. I then looked at the class to understand what was going on, after a while it dawned on me. This is what the class basically did:

class CObj
{
protected:
int iSomeNumber;
public:
int& GetSomeNumber()
{
return iSomeNumber;
}
void SetSomeNumber(int iNewVal)
{
Verify(iNewVal);
iSOmeNumber = iNewVal;
}
};

Now in code I finally found the problem:

obj.GetSomeNumber() = 42;

The code had been written by the team leader, and he wouldn't give me a good reason why iSomeNumber was protected but then he'd written a method that returned a non-const reference to it! It goes to show that senior and/or team leaders are not necessarily any better at coding than graduate engineers!

Advertisements
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s