473,854 Members | 1,425 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 11032
* 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
3736
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
1210
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
1692
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
1595
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
1671
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
2625
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
907
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
1582
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
9751
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11024
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...
1
10756
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9513
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5741
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5941
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4560
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
4159
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3186
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.