473,378 Members | 1,619 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,378 software developers and data experts.

Why assignment operator is'nt inherited?

vj
Hi Friends,

I was going through a C++ reference book when this rule caught my eye:

-->Assignment operator '=' is not inherited by the sub class.

I cannot figure out why this rule has being imposed and how it actually
makes any sense. If operator '=' cannot be inherited then it should not
be allowed to be made virtual . But following declaration on VC 6.0
compiler gets through :
/*****************************************/
class Parent
{....
public :
.....
virtual Parent & operator =(Parent & p) // !!!!
{return *this;}
};
/*****************************************/

If not inherited then why let it be a virtual function !!!

Any ideas as why is this happening?

Thanks,
VJ

Jul 1 '06 #1
7 10974
* vj:
Hi Friends,

I was going through a C++ reference book when this rule caught my eye:

-->Assignment operator '=' is not inherited by the sub class.
That is incorrect (which "reference book" are you reading?).

A copy assignment operator for a class Base does not have the signature
required for a copy assignment operator for a class Derived. It is
inherited by Derived, but does not constitute a copy assignment operator
in Derived. So even though assignment operators are inherited, just
like other member functions, that does not provide copy assignment.

I cannot figure out why this rule has being imposed and how it actually
makes any sense.


There is no such rule.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 1 '06 #2
vj

Here is the excert from the book -

"In addition, the operator= doesn't inherit because it performs a
constructor-like activity. That is, just because you know how to
assign all the members of an object on the left-hand side of the =
from an object on the right-hand side doesn't mean that assignment
will still have the same meaning after inheritance."
-- Thinking in C++, Bruce Eckel , Pg 631

Jul 1 '06 #3
* vj:
Here is the excert from the book -

"In addition, the operator= doesn't inherit because it performs a
constructor-like activity. That is, just because you know how to
assign all the members of an object on the left-hand side of the =
from an object on the right-hand side doesn't mean that assignment
will still have the same meaning after inheritance."
-- Thinking in C++, Bruce Eckel , Pg 631


Ah, the thinking Bruce... Well, just ignore the first sentence. It's
wrong (you might check if there's an errata page, these things happen).
Second Bruce: G'day, Bruce!

First Bruce: Oh, Hello Bruce!

Third Bruce: How are you Bruce?

First Bruce: A bit crook, Bruce.

Second Bruce: Where's Bruce?

First Bruce: He's not 'ere, Bruce.

Third Bruce: Blimey, it's hot in here, Bruce.

First Bruce: Hot enough to boil a monkey's bum!

Second Bruce: That's a strange expression, Bruce.

First Bruce: Well Bruce, I heard the Prime Minister use it. "It's hot
enough to boil a monkey's bum in here, your Majesty," he said and she
smiled quietly to herself.

Third Bruce: She's a good Sheila Bruce, and not at all stuck up.

Second Bruce: Here! Here's the boss-fellow now! - how are you bruce?

(Enter fourth Bruce with English person, Michael)

Fourth Bruce: 'Ow are you, Bruce?

First Bruce: G'day Bruce!

Fourth Bruce: Bruce.

Second Bruce: Hello Bruce.

Fourth Bruce: Bruce.

Third Bruce: How are you, Bruce?

Fourth Bruce: G'day Bruce.

Fourth Bruce: Gentleman, I'd like to introduce man from Pommeyland who
is joinin' us this year in the philosophy department at the University
of Walamaloo.

Everybruce: G'day!

Michael: Hello.

Fourth Bruce: Michael Baldwin, Bruce. Michael Baldwin, Bruce. Michael
Baldwin, Bruce.

First Bruce: Is your name not Bruce?

Michael: No, it's Michael.

Second Bruce: That's going to cause a little confusion.

Third Bruce: Mind if we call you "Bruce" to keep it clear?

Fourth Bruce: Gentlemen, I think we better start the faculty meeting.
Before we start, though, I'd like to ask the padre for a prayer.

First Bruce: Oh Lord, we beseech Thee, Amen!!

Everybruce: Amen!

Fourth Bruce: Crack tubes! (Sound of cans opening) Now I call upon Bruce
to officially welcome Mr. Baldwin to the philosophy faculty.

Second Bruce: I'd like to welcome the pommey bastard to God's own Earth,
and remind him that we don't like stuck-up sticky-beaks here.

Everybruce: Hear, hear! Well spoken, Bruce!

Fourth Bruce: Bruce here teaches classical philosophy, Bruce there
teaches Haegelian philosophy, and Bruce here teaches logical positivism.
And is also in charge of the sheep dip.

Third Bruce: What's New-Bruce going to teach?

Fourth Bruce: New-Bruce will be teaching political science, Machiavelli,
Bentham, Locke, Hobbes, Sutcliffe, Bradman, Lindwall, Miller, Hassett,
and Benaud.

Second Bruce: Those are all cricketers!

Fourth Bruce: Aww, spit!

Third Bruce: Hails of derisive laughter, Bruce!

Everybruce: Australia, Australia, Australia, Australia, we love you amen!

Fourth Bruce:Bruce: Crack tube! (Sound of cans opening) Any questions?

Second Bruce: New-Bruce, are you a Poofter?

Fourth Bruce: Are you a Poofter?

Michael: No!

Fourth Bruce: No. Right, I just want to remind you of the faculty rules:
Rule One!

Everybruce: No Poofters!

Fourth Bruce: Rule Two, no member of the faculty is to maltreat the
Abbos in any way at all -- if there's anybody watching. Rule Three?

Everybruce: No Poofters!!

Fourth Bruce: Rule Four, now this term, I don't want to catch anybody
not drinking. Rule Five,

Everybruce: No Poofters!

Fourth Bruce: Rule Six, there is NO ... Rule Six. Rule Seven,

Everybruce: No Poofters!!

Fourth Bruce: Right, that concludes the readin' of the rules, Bruce.

First Bruce: This here's the wattle, the emblem of our land. You can
stick it in a bottle, you can hold it in your hand.

Everybruce: Amen!

CC: Michael. Er, ..., Bruce.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 1 '06 #4
vj wrote:
Here is the excert from the book -

"In addition, the operator= doesn't inherit because it performs a
constructor-like activity. That is, just because you know how to
assign all the members of an object on the left-hand side of the =
from an object on the right-hand side doesn't mean that assignment
will still have the same meaning after inheritance."
-- Thinking in C++, Bruce Eckel , Pg 631


That's a pretty wierd statement. Assignment operators don't do
constructor like activities at all. You make mistakes if you
think about them that way as you must take care of the already
constructed things in the left hand side of the operator.

The C++ language doesn't really define inheritance. Inheritance
of member functions is just an effect of the defined behavior of
base class name resolution. The reason operator= doesn't appear
to inherit is because the fact that there's always a copy assignment
operator in the derived class (either declared expclicitly by
the programmer or implicitly by the compiler) that hides any
base class.

If you bring the name forward with a using declaration, the op
inherits as any other function:
struct base {
base& operator=(const base&) {
cout << "base op=";
return *this;
};
};

struct derived : base {
using base::operator=;
derived& operator=(const derived&) {
cout << "derived op=";
return *this;
}
};

int main(int argc, char* argv[])
{
base b;
derived d;

d = b; // prints base op=
return 0;
}
It's similar semantic rules that keep a templated function from
ever being a copy constructor.
Jul 1 '06 #5
vj
I think i will have to kick this book and send it to the dust bin :-)
....
Any ways i have one more intresting issue. Please take a look at the
code below:-

class Parent
{int m1;
public :
Parent(int m) {m1=m;}
virtual void show() {cout<<"Parent :"<<m1<<endl;}
};

class Child: public Parent
{int m2;
public:
Child(int m2,int m1):Parent(m1)
{this->m2=m2;}
void show() {cout<<"Child :"<<m2<<endl;}
void showp() {Parent::show();}
};
void main()
{ Child ch1(10,10),ch2(20,20);
Parent *pp=&ch1;
*pp=ch2; // assign obj ch2 to ch1 via pointer derefrence
ch1.show();
ch2.show();

cout<<endl<<"Parents"<<endl<<"******************** *"<<endl;
ch1.showp();
ch2.showp();
cin.get();
}
/***************************************/
Output:
Child :10
Child :20

Parents
*********************
Parent :20
Parent :20

Intrestingly only parent objects seems to be equated hoever child
object memeber m2 remains the same. I think the pointer derefence
problem again calls for virtual assignment operator function !!!

What do you think?

VJ

Jul 1 '06 #6
vj wrote:
I think i will have to kick this book and send it to the dust bin :-)
...
Any ways i have one more intresting issue. Please take a look at the
code below:-

class Parent
{int m1;
public :
Parent(int m) {m1=m;}
virtual void show() {cout<<"Parent :"<<m1<<endl;}
};

class Child: public Parent
{int m2;
public:
Child(int m2,int m1):Parent(m1)
{this->m2=m2;}
void show() {cout<<"Child :"<<m2<<endl;}
void showp() {Parent::show();}
};
void main()
{ Child ch1(10,10),ch2(20,20);
Parent *pp=&ch1;
*pp=ch2; // assign obj ch2 to ch1 via pointer derefrence
You're calling pp->Parent::operator=(ch2) here.
All that does is copy the parent subpart of the ch2 object to ch1ld
it knows nothing about the child class.

Intrestingly only parent objects seems to be equated hoever child
object memeber m2 remains the same. I think the pointer derefence
problem again calls for virtual assignment operator function !!!


You could implement it if you wanted. But what are you going to
do when the dynamic type of the left hand side of = is different
than the dynamnic type of the right hand side.
Jul 1 '06 #7
vj

You could implement it if you wanted. But what are you going to
do when the dynamic type of the left hand side of = is different
than the dynamnic type of the right hand side.
I agree to that point But is'nt this the same problem that virtual
functions were destined to solve. If this call *pp=ch would
have called 'Child::operator = ' rather than 'Parent::operator ='
then there would have being no problem as such since child would
have also equated thier encapsulated parent class objects. Thus in
effect both m1, m2 would have being equated. Hence the issue of
dynamic type of L Value could have better being handled if

---If Operator = (Parent &) is inherited ( which they are :-) )
---They were virtual so that the pointer dereference works properly

Am I correct ?

Thanks,
VJ

Jul 2 '06 #8

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

Similar topics

1
by: a eriksson | last post by:
Why is the call foo(c) below ambiguous when B inherits A privately? I think that that contradicts the first error reported below since B objects are not treated as A objects? class A {}; ...
4
by: Joachim | last post by:
How can you find out if a class is inherited from a certain other class? Is there another way than using .BaseType several times?
7
by: Daniel Kraft | last post by:
Hi all! I'm getting some for me "unexpected behaviour" from g++ 4.1.2 when compiling this simple test program: #include <cstdlib> #include <cstdio> using namespace std; template<typename...
1
by: khandelwalk1 | last post by:
here is a java code, wich is supposed to read a file name from user n read its contents and display it, bt its nt working..ne1 dere cud help me...jus copy n paste dis code in a notepad file wid name...
11
by: Fuzzyman | last post by:
Hello all, I may well be being dumb (it has happened before), but I'm struggling to fix some code breakage with Python 2.6. I have some code that looks for the '__lt__' method on a class: ...
8
by: Fuzzyman | last post by:
Hello all, I may well be being dumb (it has happened before), but I'm struggling to fix some code breakage with Python 2.6. I have some code that looks for the '__lt__' method on a class: ...
0
by: Fuzzyman | last post by:
Hello all, Sorry - my messages aren't showing up via google groups, so I'm kind of posting on faith... Anyway, I solved my problem (I think)... import sys if sys.version_info == 3:
1
by: arpan209 | last post by:
I am preparing a crystal report for library system... two tables.. 1)BookIssueDetail: fields : grno ,bookno,issuedat 2)studentMaster fileds: grno,stdename,(and lots more)..only these...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.