Computer Science Canada operator=??? |
Author: | Lone]2 [ Sat Apr 16, 2005 4:33 pm ] | ||||
Post subject: | operator=??? | ||||
hallooooo im back again with another question >.< Okay this is The code INSIDE the main program
now... inside my object declaration i have a member function
but the statement in my main program does not call the operator= .......... what am i doing wrong?[/code] |
Author: | wtd [ Sat Apr 16, 2005 4:45 pm ] | ||||
Post subject: | |||||
Let's look at what this is actually doing. It takes a constant Layout reference. That's all well and good, but what you do next is pointless. You create a new Layout object, and then immediately discard it. You then simply return this. It looks to me like you're trying to create a copy constructor. Let's look at a simple copy constructor.
|
Author: | Lone]2 [ Sat Apr 16, 2005 4:55 pm ] | ||||
Post subject: | |||||
hmm.... i do have a copy constructor... but... i want an assignment operator now..
doesn't this code calls the objects constructor? for example page = Layout(40,50); calls the page should call the operator= function... and operator= function should call the constructor (page's constructor) (i have a constructor that takes 2 of the same parameters) and i KNOW its not calling the operator= function because... when i do this:
nothing shows up on the screen (i know theres cout but i just used this to test it out, and i included stdio.h so it should work no problem) |
Author: | wtd [ Sat Apr 16, 2005 5:00 pm ] | ||||
Post subject: | |||||
No, it doesn't.
Simply creates a new object. Since you don't do anything with that object, it gets discarded immediately. My guess is that you'd want something like:
This is phenomally bad, though. You're changing the meaning of a frequently used operator where you should be using a copy constructor. Also, having height and width public is probably a symptom of bad design. |
Author: | Lone]2 [ Sat Apr 16, 2005 5:35 pm ] | ||||
Post subject: | |||||
well... i already constructed a object though... and when im done with the object... i want to 'rebuild' it with different values.... thats why im not using the copy constructor.. and thats why im using the assignment operator..... oh yea...... as for public height and width .... im IN the object...(in Layout object) so its still not valid to access the variable as "other.height"? does that mean i have to write accessor functions...??? ... so... now... i written a "set" function which should take all of my problems..... but when i do:
STILL does not work..... it DOESN'T call the assignment function... also... i tried doing this:
it gives me an compile error " 'operator =' is not a member of 'Layout' in function main ()" so... which i don't really understand.. because i have a prototype in my class...: Layout& operator=(const Layout& right); and i also defined my function: Layout& Layout::operator=(const Layout& right) { set(right.height, right.width); return *this; } XD... C++ is so troublesome |
Author: | wtd [ Sat Apr 16, 2005 5:41 pm ] |
Post subject: | |
It sounds like you really need to work on more fundamental design issues. May I see your code in its entirety? |
Author: | Lone]2 [ Sat Apr 16, 2005 5:59 pm ] | ||
Post subject: | |||
okay... well.. heres the layout object...
|
Author: | wtd [ Sat Apr 16, 2005 6:11 pm ] | ||
Post subject: | |||
Oy. Problem number 1: You're not writing C++. You're writing "C with classes". If someone is teaching you this, he or she should be fired.
Are the three most helpful lines of code I can show you. |
Author: | Lone]2 [ Sat Apr 16, 2005 6:27 pm ] |
Post subject: | |
lol .... yes... im aware im writing c code in c++.... but.... this is like a class thing.. and we just learned c... so i guess they want to make the transition from c to c++ smoother?.... and i actually found out my problem........ im so stupid.... ![]() i forgot to change my definition in my layout.h -_- how stupid of me.... |
Author: | wtd [ Sat Apr 16, 2005 6:45 pm ] |
Post subject: | |
The problem is that C and C++ are different languages. There is no transition. |
Author: | wtd [ Sat Apr 16, 2005 7:13 pm ] |
Post subject: | |
The program you're writing... could you explain what it is you're trying to do? I have no idea what WwText is, for instance. |