 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
alfredo.correa@gmail.com Guest
|
Posted: Sat Aug 20, 2005 10:29 am Post subject: Renaming inherited member, Is possible? May be good in some |
|
|
Hi,
I am definining a class called "sum" that derives from
"binary_operator". "binary_operator" has two members that are called
"argument1" and "argument2". Naturaly, if class "sum" derives from
"binary_operator" the terms of the "sum" will be forced to be refered
as "argument1" and "argument2" instead of "term1" and "term2" for
example.
It is very tempting to rename the inherited member, I never heard that
this is supported in C++ (but looking up in internet, I found that this
is supported by Eiffel language). Then I could also rename the members
to "factor1" and "factor2" in a "multiplication" class.
I think that renaming an inherited member is not desirable because it
is at least error prone. But in THIS case is SO tempting.
(Un)fortunatelly I deviced how to do the trick by creating a reference
member in the inherited class that is initialized as the inherited
member so "argument1" and "argument2" can be called "term1" and "term2"
inside the class "sum". (see code bellow)
Can any of you tell me:
0) any alternative trick?
1) is this legal?
2) is this moral?
3) is (further) inheritance-safe?
4) does the reference in the inherited class waste ANY memory? (the
size of a pointer?)
5) can you convince me not to use it in this case?
6) do you know any other example?
Thank, Alfredo
//compilable code bellow
class binary_operator{
public:
binary_operator(double a1, double a2) : argument1_(a1),
argument2_(a2){}
protected:
double argument1_;
double argument2_;
};
class sum : public binary_operator{
public:
sum(double t1, double t2) : binary_operator(t1, t2),
term1_(argument1_), term2_(argument2_){}
protected:
double& term1_;
double& term2_;
};
int main(){
binary_operator bo(3,4);
sum su(5,6);
return 0;
}
//end of code, end of message
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sat Aug 20, 2005 6:42 pm Post subject: Re: Renaming inherited member, Is possible? May be good in s |
|
|
[email]alfredo.correa (AT) gmail (DOT) com[/email] wrote:
| Quote: | I am definining a class called "sum" that derives from
"binary_operator". "binary_operator" has two members that are called
"argument1" and "argument2". Naturaly, if class "sum" derives from
"binary_operator" the terms of the "sum" will be forced to be refered
as "argument1" and "argument2" instead of "term1" and "term2" for
example.
|
Hey, our entire life is a game of monikers and synonyms. Why stop
when you're programming, right?
| Quote: | It is very tempting to rename the inherited member, I never heard that
this is supported in C++ (but looking up in internet, I found that
this is supported by Eiffel language). Then I could also rename the
members to "factor1" and "factor2" in a "multiplication" class.
|
Whatever strikes your fancy.
| Quote: | I think that renaming an inherited member is not desirable because it
is at least error prone. But in THIS case is SO tempting.
|
Isn't it? Indeed...
| Quote: | (Un)fortunatelly I deviced how to do the trick by creating a reference
member in the inherited class that is initialized as the inherited
member so "argument1" and "argument2" can be called "term1" and
"term2" inside the class "sum". (see code bellow)
Can any of you tell me:
0) any alternative trick?
|
Pointers instead of references? Use of pointers-to-members maybe?
Actually member functions.
Yes.
AFAICT, yes. It doesn't affect anybody but you and your class, does
it? If so, nothing amoral here. Whatever you do in the privacy of
your computer is entirely your business.
| Quote: | 3) is (further) inheritance-safe?
|
Sure.
| Quote: | 4) does the reference in the inherited class waste ANY memory? (the
size of a pointer?)
|
That's implementation-defined and you could find out by comparing
sizeof(binary_operator) and sizeof(sum), couldn't you?
| Quote: | 5) can you convince me not to use it in this case?
|
Probably not. Why should I waste my time trying?
| Quote: | 6) do you know any other example?
|
No. And again, I think it would be a waste of time trying to find
any. The base class is an abstraction. If you need to rename the
concepts it puts forward, it's usually done through member functions
(which might be called "accessors").
| Quote: | Thank, Alfredo
//compilable code bellow
class binary_operator{
public:
binary_operator(double a1, double a2) : argument1_(a1),
argument2_(a2){}
protected:
double argument1_;
double argument2_;
};
class sum : public binary_operator{
public:
sum(double t1, double t2) : binary_operator(t1, t2),
term1_(argument1_), term2_(argument2_){}
protected:
double& term1_;
double& term2_;
};
int main(){
binary_operator bo(3,4);
sum su(5,6);
return 0;
}
//end of code, end of message
|
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Guest
|
Posted: Sun Aug 21, 2005 2:56 pm Post subject: Re: Renaming inherited member, Is possible? May be good in s |
|
|
[email]alfredo.correa (AT) gmail (DOT) com[/email] wrote:
| Quote: | Hi,
I am definining a class called "sum" that derives from
"binary_operator". "binary_operator" has two members that are called
"argument1" and "argument2". Naturaly, if class "sum" derives from
"binary_operator" the terms of the "sum" will be forced to be refered
as "argument1" and "argument2" instead of "term1" and "term2" for
example.
It is very tempting to rename the inherited member, I never heard that
this is supported in C++ (but looking up in internet, I found that this
is supported by Eiffel language). Then I could also rename the members
to "factor1" and "factor2" in a "multiplication" class.
I think that renaming an inherited member is not desirable because it
is at least error prone. But in THIS case is SO tempting.
(Un)fortunatelly I deviced how to do the trick by creating a reference
member in the inherited class that is initialized as the inherited
member so "argument1" and "argument2" can be called "term1" and "term2"
inside the class "sum". (see code bellow)
Can any of you tell me:
0) any alternative trick?
|
How about:
#define term1 argument1
As bad as my suggestion is, I do think it compares favorably with the
current implementation.
But as Victor suggested, accessor methods let you achieve much of the
desired effect. Declare term1() and term2() in sum (though I would use
LeftTerm() and RightTerm()) and have them return the data members from
the base.
Yes.
I wouldn't argue against it on moral grounds. Though I would have no
trouble making a case for its questionable taste.
| Quote: | 3) is (further) inheritance-safe?
|
I'm not sure what that question means. Many programmers would say that
a class that has protected data members at all is probably not
following a safe design.
| Quote: | 4) does the reference in the inherited class waste ANY memory? (the
size of a pointer?)
|
I would guess that two pointer sized allocations are likely needed in
the sum class. Certainly no memory is being saved.
| Quote: | 5) can you convince me not to use it in this case?
|
I have no idea whether you can be convinced or not. But the quesion
should be the other way around. Pretend this class was part of a
collaborative effort, could you sell this idea to the other C++
programmers on the team?
The question to answer is who benefits from this design? There are two
audiences that every programmer should keep in mind when implementing a
class. The first are the class's clients, that is, programmers who will
be writing other code to call this interface. Do clients benefit? The
answer is "No", since the data members are declared protected. Clients
have no access to them. But if the names are so much better, why not
rename the parameters in the function call instead of the data members
of the class? At least that way clients can benefit by these much
better names. Who benefits when the names are inaccessible?
The answer to that question might be the programmer's second audience:
maintainence programmers. These are the programmers who assume the job
of fixing bugs or making improvements to code after its been written.
Since they did not write the original code, they will have to learn it.
Now does having two data members in a class that are aliased to two
other data members in the base make the code more readily
understandable? The answer is "no", because there are four data members
declared instead of two, but only two values stored. The fact that
their names are different actually makes the situation worse - because
it further obscures their connection. And that is the basic weakness
with this design in general, it does everything to make it look like
the pairs of data members in each class are distinct, when they are
not.
| Quote: | 6) do you know any other example?
|
Of this technique in use? No, thankfully not.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|