Catch up on stories from the past week (and beyond) at the Slashdot story archive

 



Forgot your password?
typodupeerror
×
Programming

Journal Ichijo's Journal: Inheritance Abuse

Ran across an interesting use of C++ class inheritance. In a friend's a LED-style window class, he has a dynamic array of "states" that the user of his class can populate. A state consists of a numeric identifier and a color.

So, it sounds simple. Create a structure called "state," set the members "id" and "color," and add it to the array.

But this isn't the way it was done. Instead, I (the class user) am required to inherit from the abstract class "state" and overload a virtual function to set the color. So for every different color, I have to create an entirely new class. Does this make sense?

Besides being complex and wordy (extra typing for the class user), this method has the following disadvantages:

  1. Each instance of the class adds memory for each of the member functions.
  2. There's additional overhead in calling member functions vs. retrieving data members.

I'm completely baffled. One of the only explanations I can come up with is that some people feel complex, verbose code is good code.

My other theory is that people code the way they talk. I prefer to make my comments brief and to the point. My friend takes the opposite approach, using long explanations and examples to support whatever his position is, long after he's made his point. Would this explain his coding style? Or am I just ignorant? (I'm prepared to accept both answers.)

This discussion has been archived. No new comments can be posted.

Inheritance Abuse

Comments Filter:

"No matter where you go, there you are..." -- Buckaroo Banzai

Working...