# Assignment Operator

This topic is 3623 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Why does the following setup only seems to assign value i when the assignment operator is in Class B, even when i cast myB to A. Thanks, class A { public: A &operator = (const A &rhs) { i = rhs.i; return *this; }; int i; } class B : public A { public: void operator = (const A &rhs) { i = rhs.i }; int j, k; } B myB; A myA; myB = myA

##### Share on other sites
Apparently assignment operators are not inherited. Which is news to me as well :)

This fact seems to be further verified by this snippet I wrote:

class A{public:   A &operator = (const A &rhs) { i = rhs.i; return *this; }   int i;};class B : public A{};int main(){   A a;   B b;   a = b;   b.operator=(a);}

In function 'int main()':
Line 19: error: no matching function for call to 'B::operator=(A&)'
compilation terminated due to -Wfatal-errors

##### Share on other sites
That is well dung :|, i was beginning to suspect it might be the case.

Oh well my camera system is a little less elegant than i first hoped but its not a deal killer. I am really surprised at this though, it seem to fit into the OOP ethos really well.

hhh little chuffed i found a limitation of C++, its usually so powerful. Though limitation is a bit of a stretch :p

Thanks for checking that out fpsgamer much appreciated, and kudos for finding it out so quick.

##### Share on other sites
All hope is not lost.

To fix the issue you have to use the following curious syntax:

class A{public:   A &operator = (const A &rhs) { i = rhs.i; return *this; }   int i;};class B : public A{   using A::operator=;};int main(){   A a;   B b;   a = b;   b.operator=(a);}

Quote:
 Original post by Guthurhhh little chuffed i found a limitation of C++, its usually so powerful. Though limitation is a bit of a stretch :p

I wouldn't call this a limitation. As it doesn't make much sense to automatically use a assignment operator from a base class as it implicitly doesn't know how to perform assignment for its derived parts. Therefore it makes sense to disable automatic inheritance of assignment operators and copy constructors.

##### Share on other sites
fpsgamer - You are the man :p (excuse me if you are actually not a man ;) )

i need to look up about this 'using' before class functions i have never seen that before either.

##### Share on other sites
Quote:
 Original post by Guthurfpsgamer - You are the man :p (excuse me if you are actually not a man ;) )

Yes. I am a man :)

Quote:
 Original post by Guthuri need to look up about this 'using' before class functions i have never seen that before either.

The using directive within classes is mainly used to solve the problem of shadowing.

##### Share on other sites
But i don't actually want it to assign any derived parts. I want it to copy just data for that class within objects class hierarchy. It felt natural having it in the class the data was associated with.

A function would do the job just as well, just decide on an operator instead for some reason, i'm on a learning exercise at the minute trying to get as much as i can :)

Object UVNRotor
\ /
Viewer
ProjectionView

I have an instance of Viewer and i want to assign it to an instance of ProjectionView and change the data in the Viewer portion of the object hierarchy

But to be fair you are right its not a limitation i was stretching that a bit :)

##### Share on other sites
Have you considered using composition and delegation instead of inheritance, and then just assigning the composed member?

##### Share on other sites
Zahlman - I'm feeling honest so i'll tell you if knew what composition and delegation was i may have considered it :p Though you should have seen my first implementation it makes me cringe now :S, but that was really due to a lack of understanding regarding 3D view transformation, if people would only just stop calling them cameras I would not have been so confused :p

At the moment I'm just trying to get something together that will show i can do OO Design and Coding, the lack of proof was a reason for me failing a recent interview :(, so now i'm getting my finger out :p

Thanks though i will check out composition and delegation.

##### Share on other sites
Quote:
 Original post by GuthurZahlman - I'm feeling honest so i'll tell you if knew what composition and delegation was i may have considered it :p Though you should have seen my first implementation it makes me cringe now :S, but that was really due to a lack of understanding regarding 3D view transformation, if people would only just stop calling them cameras I would not have been so confused :pAt the moment I'm just trying to get something together that will show i can do OO Design and Coding, the lack of proof was a reason for me failing a recent interview :(, so now i'm getting my finger out :pThanks though i will check out composition and delegation.

You should know when to use inheritance vs using composition before messing with operators. The latter is more or less icing on the cake, but the former is the bread-and-butter of coding. :-)

Alex

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 14
• 9
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
632913
• Total Posts
3009189
• ### Who's Online (See full list)

There are no registered users currently online

×