Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

How to do castings so, Vector3 a, b a+=b in c++?

This topic is 4888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, with c++, I am making a Vector3 class. One thing I want to add is to allow the user to add the vectors together. eg Vector3 a(1,1,1), b(3,2,1); a+=b; I heard this is called casting, but I can't find out how to actually do it, like what am I supposed to put in my class? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Casting is different. What you want here is operator overloading. += is an operator, and you can add a function to your class (or separate from your class, if you want) to tell the compiler how that operator works with your class.

You can do it something like this:

class Vector3
{
// ... the rest of your vector class ...
public:
Vector3 &operator += (const Vector3 &rhs)
{
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
};


You don't have to give it that return type, but it is considered normal to do so, because the standard assignment operators for built in types like int will return (as the result of the expression) the value that was assigned.

John B

Share this post


Link to post
Share on other sites
it is not &operator, this means the operator returns a vector3 & which is a reference to a vector3.

Also, the return *this in JohnBSmall's post is used for having multiple operator on the same line like this: vec1 += vec2 += vec3;

hope that helps !
Matt

Share this post


Link to post
Share on other sites
Quote:
Original post by johnnyBravo
I see thanks.

So I guess its best to use JohnBSmall's reference way to avoid creating new memory for the variables?

thanks


Only for the assignment operators (including += and friends).

Here is an example of two correct operators for my 2D vector class:


class AV2i
{
// stuff...

inline AV2i& operator+=(const AV2i &v) {x += v.x; y += v.y; return *this;}
inline AV2i operator+(const AV2i &v) const { return AV2i(x+v.x, y+v.y); }

// more stuff...
}





Just a run down of what happens:

- "inline" - tells the compiler to compile the function inline. Not generally necessary these days (particularly MSVC 2003 ignores it).

- AV2i& is returning a reference to a vector, AV2i is returning the vector object itself.

- They both take "const AV2i& v" - this indicates that they take a referene to a vector and that the vector that is passed in is not changed. This is generally the best way to pass unchanging, complicated types around.

- "return *this" dereferences the object that is being added to and returns it.
- "return AV2i(...)" creates a new object and returns it.

Those two different returns and return types are required so your class matches the built-in types in the way that it operates. All the assignment operators should return a reference to "*this", and all the operators arithmatic should return a fresh object by value. (Again, this is generally and if you want to match behaviour of the built-in types).

Share this post


Link to post
Share on other sites
Hey I didn't realise i could call the Vector instance's variables without going through the get/set functions. eg if im inside the vector class, and want the x value from a Vector class instance, i can call just v.x instead of v.getX().

Well anyway, I've got a warning that won't go away:
Quote:

vector.cpp(74) : warning C4172: returning address of local variable or temporary


from the code:

Vector &Vector::operator + (const Vector val) {
return Vector(x+val.x, y+val.y, z+val.z);
}




Any ideas? Thanks

edit:
Oh and also the D3DXVECTOR3 structure can do things like this:
D3DXVECTOR3 abc(1,2,3);
D3DXVECTOR3 v = 5.0f + abc;

Where as mine can't do that, but only can only do:
v = abc + 5.0f;

I don't get what I can do to be able to do that.

Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by johnnyBravo
Hey I didn't realise i could call the Vector instance's variables without going through the get/set functions. eg if im inside the vector class, and want the x value from a Vector class instance, i can call just v.x instead of v.getX().


Yes you can provided they aren't private variables. If they are private then you need to use v.getX();


Quote:
Original post by johnnyBravo
Well anyway, I've got a warning that won't go away:
vector.cpp(74) : warning C4172: returning address of local variable or temporary

from the code:
*** Source Snippet Removed ***


Thats because you are returning reference to a local variable which you never ever want to do. The reason is, the value is in scope only until the function is executing, after that you will be pointing to junk. If you want to return a local variable, do a copy. The right way to do what you are doing is.


Vector Vector::operator + (const Vector val) {
return Vector(x+val.x, y+val.y, z+val.z);
}




If you want to return a reference, do it the way JohnBSmall is doing it. In Andrews post you can see this subtle difference already.

Hope this helps.






Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!