Overloading the = operator

Still studying for a final.

Here is the question...

Please provide a function that overloads the = operator for a fancyString class. The fancyString class has members: int size; string prevstring; string curstring;. The prevstring is the string that was previously stored in case its retrieval is needed.


Here is my attempt...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 class fancyString{
private:
	int size;
	string prevstring;
	string curstring;
public:
	fancyString();
	fancyString(type stuff); // this is just a basic idea 
	fancyString &operator=(fancyString rhs); 
};

fancyString &fancyString::operator=(fancyString rhs){
	
	prevstring = curstring;
	curstring = rhs.curstring;
	size = rhs.size;
       
        return this;

} 


I'm fairly confident I'm wrong, but that's my best attempt so far. Can someone steer me back on track?

Thank you.

(this is just a practice exam. You're not helping me cheat.)
Last edited on
assignment implies that after it happens, the guy on the left side is absolutely equal to the input guy, for most classes. Close... but why did you have prevstring different? I would have said..


1
2
3
4
5
6
7
8
9
10

fancyString &fancyString::operator=(fancyString rhs)
{
	
	prevstring = rhs.prevstring;
	curstring = rhs.curstring;
	size = rhs.size;
         return this;
} 
Last edited on
>> but why did you have prevstring different

See, this is what kinda had me confused. It's more of a general confusion about objects period, but you inadvertently gave me insight.

I was thinking that if rhs.curstring was passed into the function, there couldn't be a rhs.prevstring at the same time.

But that's not the case is it. It can be any object.

That's so confusing.

Are overload functions not really functions at all then?

Am I making sense?
rhs is its own object.
surely (well, if you pretend everything is public, anyway ) you can see this:

fancyString x;
fancyString y;
x.prevstring = y.prevstring;
and so on, right?
that is ALL that is happening in your assignment operator.
it has an input (call it y for the above analogy) and it has itself (call that x for the analogy). It takes a while to get used to the 'I am of this type and have these things and what was passed in also has the same things' coding, but that x and y example is all you are really doing once you get past the syntax.

------------------
Yes, overloads (and class operators) are indeed functions. The operators are sort of *special* functions (their syntax CAN be different). Want to see something really screwy... I don't know if I will get it right, but you can actually invoke them 'normally'. x.operator=(rhs); //its very close to this, I haven't done that in a decade or more .. that is the same as x = rhs; The language understands that they are operators (theres a list of operators, you can't make new ones) and allows some shortcuts when calling them but they ARE functions (for classes. for some types, like int, the 'function' is a CPU operation or two).

and standard overloads are obviously functions (same name, different parameters, both user written code).
Last edited on
ohhhhhhh. (fancystring rhs), that's....(type name) or (int x). How in the world is that just now making sense to me?

Thanks man.

#include facepalm

don't be too hard on yourself. The syntax of objects / OOP is hard to get used to, and that is ON TOP of the concepts which go deep and are a lot to absorb.
Topic archived. No new replies allowed.