Jump to content

Photo

Operator Overloading and Casting

C++11

  • Please log in to reply
9 replies to this topic

#1 Mero

Mero

    Touch Fluffy Tail

  • Banned
  • Real Name:Tamamo No Mae
  • Location:Rainbow Factory

Posted 22 April 2013 - 09:57 AM

I'm having trouble with grasping the concept of operators it seems. Could someone please explain the concept behind both of these when it comes to object oriented programming.

#2 MoscowModder

MoscowModder

    Sometimes lurking. Rarely posting.

  • Members
  • Location:Wisconsin

Posted 22 April 2013 - 12:00 PM

What are you having trouble undstanding? Are you making custom classes, or just overloading operators for an existing class?

Is it the concept or the implementation you're stuck on?

#3 aaa2

aaa2

    Wizard

  • Banned

Posted 22 April 2013 - 12:04 PM

I'm having trouble with grasping the concept of operators it seems. Could someone please explain the concept behind both of these when it comes to object oriented programming.

Maybe in the example of the + operator. Ussually + is being used to add up numbers. But now you could want to add up lets say vectors since writing add(vector1, vector2) as a function is tedious and looks very annoying what you do is you give the operator + a second meaning for vectors. Giving the operator a second meaning is what you call overloading it. So defining addition for vectors using + is operator overloading. You can now do the same for matrices etc. You could also define that operator to say add up pictures(which is essentially matrix addtion but whatever).

#4 Mero

Mero

    Touch Fluffy Tail

  • Banned
  • Real Name:Tamamo No Mae
  • Location:Rainbow Factory

Posted 22 April 2013 - 12:23 PM

Okay I understand that I think what your saying.
Basically it's easier and the code is more understandable to overload the operators rather then write a function?

#5 MoscowModder

MoscowModder

    Sometimes lurking. Rarely posting.

  • Members
  • Location:Wisconsin

Posted 22 April 2013 - 12:29 PM

Actually, operators are functions. When you use an overloaded operator, it calls a function you wrote. But yes, x + y is easier to read than add(x,y).

#6 Saffith

Saffith

    IPv7 user

  • Members

Posted 22 April 2013 - 12:32 PM

Yeah. Operator overloading allows a class to use a built-in operator for a non-standard purpose. For instance, std::string overloads += so that it acts as an append operator.
std::string str("abc");
str+="defg"; // Now str is "abcdefg"
Normally, += is only used to add numeric data; using it on strings wouldn't be legal if the string class didn't explicitly define what that does.


As for casting, that refers to interpreting one type of data as another type. That might mean either reinterpreting the data or converting a pointer or reference to another type.
One of the most common examples is adding numbers. It's not possible to add, say, an int and a float; their internal representations are incompatible. In order to add them, one has to be converted to the other. When you write 2.5f+3, the compiler treats that as 2.5f+static_cast<float>(3). In this case, although the two types are different internally, there's a clearly defined way to convert between them. If there weren't, it would fail to compile.
It's also relevant to polymorphism.
Weapon *w=new Sword();
w.stab(); // Won't compile; w is a Weapon pointer, and stab() isn't defined for all weapons
dynamic_cast<Sword*>(w)->stab(); // This is okay
That's not a matter of different internal representations; the compiler just needs to be told what data type you think you're working with.

Incidentally, you can also overload casts in C++:
class SomeClass
{
public:
    operator int()
    {
        return 42;
    }
};

// ...
SomeClass sc;
int x=sc; // x is 42


#7 Mero

Mero

    Touch Fluffy Tail

  • Banned
  • Real Name:Tamamo No Mae
  • Location:Rainbow Factory

Posted 22 April 2013 - 12:37 PM

I understand it now. Thanks guys. :)

#8 Gleeok

Gleeok

    It's dangerous to dough alone, bake this.

  • Members
  • Real Name:Pillsbury
  • Location:Magical Land of Dough

Posted 23 April 2013 - 08:06 PM

I would also like to add that if you aren't sure whether something should be a method or an operator, then it definitely should not be an operator. Ex;
vec2 a, b = polar(90);
vec2 c = a ^ (b >> b).distance();
if( -b & 3.f > PI )
  return c | b;

Looks spiffy right, but there's only two things there that make any sense at all; "polar" and "distance"; the rest is complete garbage to everyone except perhaps the person that "invented" those operators in the first place.

#9 Mero

Mero

    Touch Fluffy Tail

  • Banned
  • Real Name:Tamamo No Mae
  • Location:Rainbow Factory

Posted 23 April 2013 - 09:19 PM

Absolutely, readability is very important. Especially in coding.

#10 lucas92

lucas92

    Defender

  • Members

Posted 26 April 2013 - 04:59 PM

using namespace std;
//let's say a double that we want to convert to a int
double i = 1.2;
//used in C. You shouldn't use it in C++
int u = (int)i;
//the C++ way
int v = static_cast<int>(i);
//Child derives from Parent
Parent* object = new Parent();
try{
//trying to change Child* to any other pointer type will trow an error
typedef Child* type;

if(typeid(type) != typeid(Child*))
throw typeid(type);

//converting a parent object to a child object pointer
object = dynamic_cast<Child*>(object);
}
catch(const string e){
cout << e << " is not the child type" << endl;
}

You can also use const_cast to change a constant expression to a non constant one.

Edited by lucas92, 26 April 2013 - 05:00 PM.




Also tagged with one or more of these keywords: C++11

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users