Reputation:
Vec2& operator+=(Vec2 const& v);
and
Vec2 operator+(Vec2 const& u , Vec2 const& v);
im supposed to create functions in a class for each of those lines. there are even more but if someone could explain me what they are about, im sure of it that i can do the rest on my own.
so what are my ideas? Vec2 is the name of my class probably meaning this operator is an object of this class. but at the same time this doesnt make any sense, because there is this " += " that shows to me this is a line where you basically just do this:
Vec2& operator = Vec2& operator + Vec2 const& v
but i guess this is probably wrong. my next guess was this is a function that should start with { inserting the body instead of this ;
the second one looks to me like its a function for real, but why is a plus following the operator. does this mean this is a special way to name a function and the plus doesnt actually mean anything?
i hope someone can explain me what this is about. im sorry to bother you with such a specific question :/
Upvotes: 0
Views: 80
Reputation: 28679
Those are function declarations, and your task is to implement the definitions.
Vec2
is the class type. operator+
and operator+=
are operator overloads. Operator overloads are syntactic sugar, in that they allow you to type vec_a += vec_b;
and where +=
is, the compiler replaces this with a call to operator+=
.
Vec2 vec_a;
Vec2 vec_b;
vec_a += vec_b; // the compiler calls vec_a.operator+=(vec_b)
You can do anything in your function, but convention (and principle of least surprise) dictates +=
results in the contents of vec_b
being appended onto the end of vec_a
.
Vec2 vec_a;
Vec2 vec_b;
vec_a += vec_b; // vec_a now contains its contents *and* vec_b's contents
Your task is to provide the code which will achieve this expectation; that is, you need to write code so that the contents of the right hand side of the expression (vec_b
in my example) are appended onto the end of the contents of the left hand side of the expression (vec_a
in my example).
As an example, let's say Vec2
has the following data members:
int* data; // a pointer to array of ints, "capacity" long
int capacity; // the length of the array of ints
int size; // the number of ints stored in data
You would then provide the definition as something like the following:
Vec2& Vec2::operator+=(Vec2 const& v)
{
int required_capacity = size + v.size;
if (required_capacity > capacity)
{
// create a temp array required_capacity long
// copy contents of data into temp array
// assign data so it points to temp
// assign capacity to required_capacity
}
// copy v.size elements from v.data to data[size]
// increment size by v.size
return *this;
}
The actual implementation I leave to you as an exercise
Upvotes: 2
Reputation: 2408
This is a definition of a function for an operator. C++ doesn't know your types, and it also doesn't know what to do with your tipes if you want to use a mathematical operator on them.
this line:
Vec2 operator+(Vec2 const& u , Vec2 const& v);
declares that for your 'Vec2' type, there is a mathematical '+', so you can use
Vec2 v = yourVector + yourVector2
whitout errors. Also, please take a look here
Upvotes: 0
Reputation: 642
These are indeed function declarations for the class Vec2
, which are used to declare possible operators for this class (called operator overloading). If you define them, you can use the +
and +=
operator on your class, just like with builtin types.
Vec2 a, b; // initialize somehow
Vec2 x = a + b;
x += b;
You can basically overload almost all operators. For more information see: http://en.cppreference.com/w/cpp/language/operators
Upvotes: 0