SRL Archive - C/C++ Help and Tutorials

overloading operators


Feroc1ty

2010-10-14 00:38:58

attributes attributes::operator+(item & rhs)
{
attributes temp = this;
}

This won't compile, I'm not sure why… most of the crap is left out but attributes and item are both classes (obviously), why can't I set the temporary attributes be set to the "This" argument?

Is there another way of achieving rather than doing the lengthy:

temp.something = this.something;
temp.something2 = this.something2;

The class is quite big so it would be very tedious to do so =/.

Thx for taking your time reading this and for any potential input.

HyperSecret

2010-10-14 02:25:28

lets me ask you this…

how is the + operator doing anything about adding when all you do it = something? I think you have your operators confused…

Also, it is always good to pastebin your classes and post links to them, so we can help you more.

Also what compile errors are you getting? I would assume you can't do the above is because there is no overloaded operator= for the attribute class so how does it know what to equal when equaling that class?


Please be more clear in what you want…

Feroc1ty

2010-10-14 05:50:02

http://pastebin.com/ZsVJuSXa

Line 124.

Nava2

2010-10-14 08:23:01

Why not just use ``this'' instead of ``temp=this'' seems redundant to me..?

Feroc1ty

2010-10-14 16:12:09

From: Nava2
Why not just use ``this'' instead of ``temp=this'' seems redundant to me..?


Don't you have to return the class if you over the binary constructors?

Hmmm… I guess I could make a custom copy constructor for the attributes class, I'll try and post an update.

HyperSecret

2010-10-14 19:26:02

What errors are you getting?

You're code is very inefficient every time you create a new class temp in your operators just to return temp.

What are you making?

Feroc1ty

2010-10-14 22:56:10

As if memory is a problem these days…

It's simply an on-going project that I'm doing when I get bored and learn something from my C++ book, nothing extraordinary.

Plus I usually finish my projects, and after they do what they're supposed to do I optimize them, no point in getting lost in the details of a program when all you want it to do is compile and do whatever tasks you need it to do.

johnpitter

2011-03-14 01:21:49

Operating overloading means more than one meaning, or "overloads" it. You can redefine the function of most built-in operators globally or on a class-by-class basis. Overloaded operators are implemented as functions.