[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Can someone please explain OPERATOR OVERLOADING?
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 9
Thread images: 1
File: c++.png (7 KB, 375x375) Image search: [Google]
c++.png
7 KB, 375x375
Please, someone, this is so hard, explain LINE by LINE please, what each thing does etc
>>
>>55044596
no, use google and wikipedia
>>
If I have something, of type "string"

What does adding them together mean?

Does it mean that I want to add each individual character's byte value together numerically? Of course not, that's retarded.

It means "concatenate"- place one after another and give me a new copy. "Fuck " + "you" becomes "Fuck you"

If you were implementing your own string class from scratch, you would need to overload the + operator to do concatenation for strings.
>>
>>55044596
standard operator does one thing
write a function with the same name
function does another thing
now operator does another thing
>>
>>55044596

> this is so hard

It's not hard, you are just stupid.


Overloading means you give a symbol a new meaning.

For example you make 3d-objects a = (1,2,3) and b = (4,5,6), you can overload the "+" operator so you can add them like that:
c = a + b

Now do your own reasearch, fag.
>>
CONDUCTOR
>>
>>55044621
Off-topic from the main question, wouldn't operator overriding be a more appropriate term for this idea?
>>
>>55044775
It's overriding from the type's perspective (you override the behaviour it exhibits when that operator is applied to it), but from the operator's perspective it's overloading because it can still exhibit its old behaviour for different types.

I'd guess that's the reason for the naming at least.
>>
>>55044596
>Please, someone, this is so hard, explain LINE by LINE please, what each thing does etc

Ok, take for example

BigInt operator++(){
return (*this)+=1;
}

>BigInt

return type

>operator++

Which operator I want to overload. The word "operator" is prefixed to make compiler writers' lives easier.

>()

empty means it's the prefix version. (int dummy) would be the postfix version.

>return

What I'm giving back

>(*this)

the class I'm incrementing accessed by the "this" pointer

>+=

add the right to the left and overwrite the left value operator

>1

The number after 0

>;

End of instruction marker

>{ ... }

The code block
Thread replies: 9
Thread images: 1

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.