I have seen these used before, however I don't understand what they are used for. As they are rarely spoken about in beginning tutorials, books etc and I'm having trouble finding reference on them.
1 2 3
& | ~ ^
and
^= &= |= <<= >>= []
I looking at the table on http://www.cplusplus.com/doc/tutorial/classes2/
About overloading operators.
I assume, that the [] is the array operators?... There's also -> this, and () parenthesis operators is this right?
However I'm uncertain about the others.
hmm ok thanks, so all the others are as I thought? I don't see how you could overload [], If it's a constant pointer so to speak, what exactly could you overload it to? the multiplication operators are easy, and make sense as you can specify different members you want to math, or in the case of = specify only certain members to assign. But things like [] ,, Is well beyond my logical train of thought ...
[] is overloaded usually if the class represents some sort of array, or needs indexing (for example std::vector).
I usually overload () as a do_it() method. Since with () you can arbitrary parameters, it can have a lot of uses.
-> for example is overloaded for iterators (this is the pair of the dereference (*) operator).
I usually overload () as a do_it() method. Since with () you can arbitrary parameters, it can have a lot of uses.
I assume by arbitrary you mean mathematics: undetermined; not assigned a specific value.
Aren't parameters arbitrary by nature? I don't really get that. I mean it's in the case of objects so say we have a circle object we would probably have a blank constructor, and one that takes radius. create a constructor that takes another circle? What then could you overload () to do ?
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
/*
Shifting number right 3 positions with the expression (number >> 3) divides the
value by 8 and discards the remainder.
Shifting the result of the previous operation left by 3 positions with the
expression ((number >> 3) << 3) multiplies
it by 8 so you get the value of number less the remainder R after dividing by
8 i.e. (number - R)
Subtracting (number - R) from number gives just R, which is what you are
looking for.
ex. given: 12 = 00001100
12>>3 = 1 = 00000001
1<<3 = 8 = 00001000
12-8 = 4 = 00000100
*/
int main()
{
int number;
cout << "Enter a number: ";
cin >> number;
cout << "Thank you. The remainder after dividing your number by 8 is "
<< number - ((number >> 3) << 3);
cout << endl;
return 0;
}