#1
Posted 22 April 2013 - 09:57 AM
#2
Posted 22 April 2013 - 12:00 PM
Is it the concept or the implementation you're stuck on?
#3
Posted 22 April 2013 - 12:04 PM
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).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.
#4
Posted 22 April 2013 - 12:23 PM
Basically it's easier and the code is more understandable to overload the operators rather then write a function?
#5
Posted 22 April 2013 - 12:29 PM
#6
Posted 22 April 2013 - 12:32 PM
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 okayThat'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
Posted 22 April 2013 - 12:37 PM
#8
Posted 23 April 2013 - 08:06 PM
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
Posted 23 April 2013 - 09:19 PM
#10
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
→
Computers and Technology →
Move Constructors and Assignment OperatorsStarted by Mero , 01 May 2013 |
|
|
||
→
Computers and Technology →
Need some OOP excercisesStarted by Mero , 29 Apr 2013 |
|
|
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users

