473,778 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11028
* 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<<"Pa rents"<<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::operat or=(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::operato r = ' rather than 'Parent::operat or ='
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
3730
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 {}; class B : private A {}; class C : public B, public A {};
4
1207
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
1687
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 t>
1
1591
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 TsetofFile.java n save another input notepad file in bin to give it as input to code. Execute this code n give dat file name...bt its showng File nt found..watz rong in dis...plz help me asap...!! import java.io.*; import java.net.*; class...
11
1663
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: if hasattr(clr, '__lt__'): However - in Python 2.6 object has grown a default implementation of
8
2616
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: if hasattr(clr, '__lt__'): However - in Python 2.6 object has grown a default implementation of
0
903
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
1578
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 filed are req to display...
0
10298
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10127
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7475
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5500
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4033
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2865
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.