Sign in to follow this  

Assignment Operator

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

If you intended to correct an error in the post then please contact us.

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 this post


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

More info: http://bytes.com/forum/thread63241.html

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 this post


Link to post
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 this post


Link to post
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 Guthur
hhh 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 this post


Link to post
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 this post


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


Yes. I am a man :)

Quote:
Original post by Guthur
i 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Guthur
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.


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

Share this post


Link to post
Share on other sites
I feel pretty comfortable with inheritance, just never heard of composition. Didn't really think operator overloading was to heavy to be honest, possible memory management issues asside. I don't use it much though to be fair, first time on my current project and its got rather big already :p

I would hate tp tackle large projects without even my basic OO coding principles.

Share this post


Link to post
Share on other sites
Though as we were discussing the design...

The viewer class actually ends up being an empty class, the members i actually wanted to assign were inherited themselves; the axis rotations from UVNRotor and Positional information from the Object (actually RenderObject). What I was going to end up with was a camera representation, Viewer, that I could place within my RenderObject Tree (linked list tree) these object would be switched on by the rendering engine using RTTI. I have this weird idea of cameras appearing through out a my renderobject list to define new viewing arrangements...but if ever there was solution looking for a problem this might be it :S.

But like i said i'm kind of on a learning exercise and this is my first 'real' graphics engine, the learning curve is very steep. Spent the last 2 days grappling with vector maths and View transformation matrices.

Share this post


Link to post
Share on other sites
Quote:
Original post by Guthur
I feel pretty comfortable with inheritance, just never heard of composition.


A fancy name for making an object of one class a member of another class.

Here's why it's important: in designing a system you frequently have to decide whether you want derive from a class to make use of its interface or make it a class member instead.

Generally creating large inheritance trees is shunned. Usually one creates a forest of small trees instead and composition is key to that, i think.

Quote:

Didn't really think operator overloading was to heavy to be honest, possible memory management issues asside. I don't use it much though to be fair, first time on my current project and its got rather big already :p

I would hate tp tackle large projects without even my basic OO coding principles.


Did you read Scott Meyer's Effective C++ yet? :-)

Alex

Share this post


Link to post
Share on other sites
AK-73: Haven't had the chance to read that book unfortuately, seems to get good reviews on amazon. I have to admit I really haven't read enough programming books, couple of reference books, and then good old Google :p. I have to admit i find a computer based resource better for immediate needs, i should try and cover abit more abstract theory which is probably better got from books.

Share this post


Link to post
Share on other sites
Quote:
Original post by Guthur
AK-73: Haven't had the chance to read that book unfortuately, seems to get good reviews on amazon. I have to admit I really haven't read enough programming books, couple of reference books, and then good old Google :p. I have to admit i find a computer based resource better for immediate needs, i should try and cover abit more abstract theory which is probably better got from books.


Well, it's not abstract theory. The problem is that books and articles teaching the language only teach *what* *can* be done in C++.

Effective C++ teaches *how* it *should* be done. What you shouldn't do (because it might come back to bite you) and how you should do handle some things instead (if you're smart).

If you ever plan to work on a bigger project or even to turn professional, it's a must-read, I think. Otherwise you can learn about some of the pitfalls only the hard way.

Or in other words: if you ever wondered about how professional coders write their code and why they do it that way, this is the book to go. In my mind it's the book to read right after one has learnt the entire (or most of it) langauge.

Surely has painted C++ in a different light for me. :-)

Alex

Share this post


Link to post
Share on other sites
Thanks for the tip AK, will definitely mark it down as something to look into. There is so much to know i wonder if one life time is really enough, might ask for a free continue :p

Share this post


Link to post
Share on other sites
Quote:
Original post by Guthur
Thanks for the tip AK, will definitely mark it down as something to look into. There is so much to know i wonder if one life time is really enough, might ask for a free continue :p


You play it one level at a time, one after another. :-)

Besides, the coders who wrote some of the best games in history certainly didn't know everything there is to know either. Some may have known *a lot* but... one doesn't need to know everything to be a successful coder. It's just that the more tools you have, the better your chances at being successful are. :-)

Alex

Share this post


Link to post
Share on other sites
Quote:
Original post by ak-73
it's just that the more tools you have, the better your chances at being successful are. :-)


I'm getting the Powerups were ever i find them :p. I'll admit i have been lazy, never really pushed myself enough with the programming. Hoping to put that right now.

Share this post


Link to post
Share on other sites
I don't understand why the terminology should be such a stumbling block. If you know what "compose" means, you know what "composition" means. There's nothing arbitrary about the term; it derives pretty much directly from its normal English meaning.

Share this post


Link to post
Share on other sites
Zahlman: umm ya you are right, but my derogratory tone towards it was quite tongue in cheek. At the time i did suspect i was probably aware of what the term referred to, i still wasn't sure what exactly it was, and a quick google didn't make the connection between the term and what I refer to it as, which is by relation 'has a'.

Though the by relation, 'has a', description is possibly not as correct a description in all cases as composition it suits for most cases.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this