473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Idiom for modifying base behavior?

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.)

Jul 22 '05 #1
5 1274
"jeffc" <no****@nowhere.com> wrote in message
news:40********@news1.prserv.net...
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.
Jul 22 '05 #2
"jeffc" <no****@nowhere.com> wrote in message
news:40********@news1.prserv.net...
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.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #3
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:3FYlc.153017
"jeffc" <no****@nowhere.com> 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?
Jul 22 '05 #4
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message news:<6i********************@bgtnsc05-news.ops.worldnet.att.net>...
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:3FYlc.153017
"jeffc" <no****@nowhere.com> 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?

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?
Jul 22 '05 #5
"A. Lloyd Flanagan" <al************@comcast.net> wrote in message
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?
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: pytopo | last post by:
I like the way call tips displays argument variables for functions when you type the "(" after the function name. However, if one of the arguments to the function is something like...
7
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding Herb Sutter's idiom of implementation of operator= via nonthrowing swap() member function, to guarantee strict exception safety. The idea of the idiom is...
6
by: Gareth Stockwell | last post by:
I've been using an technique whereby a tag used as a parameter to a template class C determines which of several potential base classes (A,B) C inherits from. What I want to know is whether there...
3
by: Martin | last post by:
I'm trying to paint my own caption bar/title bar and also my own frame border, but I'm having problems with modifying the clip region so windows doesn't paint over my stuff when I call base.WndProc...
9
by: Christian Hackl | last post by:
Hi! I've got a design question related to the combination of the NVI idiom (non-virtual interfaces, ) and popular object-oriented patterns such as Proxy or Decorator, i.e. those which have the...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
9
by: koschwitz | last post by:
Hi, I hope you guys can help me make this simple application work. I'm trying to create a form displaying 3 circles, which independently change colors 3 times after a random time period has...
12
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
5
by: IUnknown | last post by:
Ok, we are all aware of the situation where modifying the folder structure (adding files, folders, deleting files, etc) will result in ASP.NET triggering a recompilation/restart of the application....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.