Idiom for modifying base behavior?

J

jeffc

Don't know what to put in the subject, because if this has a name I don't
know it.

You are modifying an existing application. You are not allowed to make more
than minor changes. For example, there is class B, which is instantiated by
class A. You wish to modify B, so you derive B2. You are allowed to make a
minor change such as replacing the raw "new" in class A with a "create"
function. You derive A2 from A and override this function, and instantiate
B2 instead of B.

There are multiple classes such as A that instantiate a B. However, you
come across 2 of them that instantiate a B3 instead of a B (B3 derives from
B). Now you have a situation where you need to alter both the behavior of B
and B3. You have to create B4 to derive from B3. But you put the exact
same behavior in B4 as you did in B2, which you want to avoid for obvious
reasons.

B
|\
| \
| \
| \
B3 B2
|
|
B4

In some cases you'll want to create a B4, and in some cases a B2. Is there
any way to get your class "inserted" into the hierarchy? You are not
allowed to alter the class the B3 derives from. (By the way, multiple
inheritance is not supported.)
 
S

Siemel Naran

You are modifying an existing application. You are not allowed to make more
than minor changes. For example, there is class B, which is instantiated by
class A. You wish to modify B, so you derive B2. You are allowed to make a
minor change such as replacing the raw "new" in class A with a "create"
function. You derive A2 from A and override this function, and instantiate
B2 instead of B.

There are multiple classes such as A that instantiate a B. However, you
come across 2 of them that instantiate a B3 instead of a B (B3 derives from
B). Now you have a situation where you need to alter both the behavior of B
and B3. You have to create B4 to derive from B3. But you put the exact
same behavior in B4 as you did in B2, which you want to avoid for obvious
reasons.

Unfortunately, I think you have to reproduce the class the
functionality of class B2 in B and B3. The best I've come up with for
this is recursive derivation, like so:

template <class ActualB>
class B2extra {
public:
void f() const;
};

template <class ActualB>
void B2extra<ActualB>::f() const {
const ActualB& me = static_cast<const ActualB&>(*this);
...
};

class B2 : public B2extra<B2> { ... };

You can engineer it further to avoid code bloat.
 
C

Cy Edmunds

jeffc said:
Don't know what to put in the subject, because if this has a name I don't
know it.

You are modifying an existing application. You are not allowed to make more
than minor changes. For example, there is class B, which is instantiated by
class A. You wish to modify B, so you derive B2.

[snip]

Let's stop right there. There IS a name for the idiom you're using. It's
called "hacking".

If you want to modify B, modify B. If you have a boss who objects, tell him
it is "refactoring" which is a Good Thing. If you persist on your current
course you are going to make an unmaintainable mess.
 
S

Siemel Naran

Cy Edmunds said:
"jeffc" <[email protected]> wrote in message
Let's stop right there. There IS a name for the idiom you're using. It's
called "hacking".

Fair enough.
If you want to modify B, modify B. If you have a boss who objects, tell him
it is "refactoring" which is a Good Thing. If you persist on your current
course you are going to make an unmaintainable mess.

OK, but the real world doesn't seem to work that way. Besides, what if you
bought a third party library?
 
A

A. Lloyd Flanagan

Siemel Naran said:
Fair enough.


OK, but the real world doesn't seem to work that way. Besides, what if you
bought a third party library?


One big reason multiple inheritance is supported in C++ is to allow
you to use third-party libraries without having to get into this mess.
Absent multiple inheritance or access to the base class, you've got
no other way to do it but to duplicate functionality in multiple
classes.

If you're coding in C++ but the environment you're in doesn't permit
you to use some of the major features of C++, you probably need to
consider changing environments. What's the point of driving a Ferrari
if you're never allowed to go faster than 20 mph?
 
S

Siemel Naran

A. Lloyd Flanagan said:
One big reason multiple inheritance is supported in C++ is to allow
you to use third-party libraries without having to get into this mess.
Absent multiple inheritance or access to the base class, you've got
no other way to do it but to duplicate functionality in multiple
classes.

Can you illustrate how multiple inheritance solves the OP's problem?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top