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

Started by
19 comments, last by snk_kid 18 years, 10 months ago
Quote:Original post by johnnyBravo
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.

First of all, are you sure it is an addition? You probably meant multiplication.

Think about it like this. When you are making your operator that takes a float and scales your vector, you probably have something similar to this:
Vector3 Vector3::operator * (float s){ return Vector3(x*s, y*s, z*s); }

This is equivelant to a global function like this:
Vector3 MultiplyVector3ByScalar(const Vector3& lhs, float rhs){ return Vector3(lhs.x*rhs, lhs.y*rhs, lhs.z*rhs); }

This means that you can only multiply the vector by a scalar to the right of it.

In order to multiply it by a scalar to the left, you need to make a global operator, like so:
class Vector3 {...    friend Vector3 operator * (float lhs, const Vector3& rhs);    float x, y, z;}inline Vector3 operator * (float lhs, const Vector3& rhs){ return Vector3(rhs.x*lhs, rhs.y*lhs, rhs.z*lhs); }

Now, you can multiply by a scalar to the left and the right.
Vector3 test1(3.0f, 4.0f, 5.0f);Vector3 test2 = test1 * 3.0f; // Multiply by a scalar to the righttest1 = -3.0f * test2; // Multiply by a scalar to the left


Hope I wasn't beaten to the reply ;).

[EDIT] Forgot the inline keyword and added a little more to the explanation. And, I guess I wasn't beaten to the reply.

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


Private refers to the class, not the instance. For example:
class X{private: int a;public:void somefunc(X someotherx){ a += someotherx.a; //Fine, even though the variable is private.}};


Also, MATHMATICAL VECTORS SHOULD NOT HAVE GET AND SET FUNCTIONS FOR THE DIMENSIONAL COMPONENTS.
Quote:Also, MATHMATICAL VECTORS SHOULD NOT HAVE GET AND SET FUNCTIONS FOR THE DIMENSIONAL COMPONENTS.


Oh, I thought I should follow the class convention....so I should make them public instead?

thx

edit:

Oh and Slaru, I'm getting this error:
Quote:
vector.cpp(121) : error C2511: 'Vector Vector::operator *(float,const Vector &)' : overloaded member function not found in 'Vector'
c:\my\new\space\vector.h(2) : see declaration of 'Vector'

for this code:
inline Vector Vector::operator * (float lhs, const Vector& rhs) { //this line is the problem 	return Vector(rhs.x*lhs, rhs.y*lhs, rhs.z*lhs); }

Quote:Original post by johnnyBravo
Quote:Also, MATHMATICAL VECTORS SHOULD NOT HAVE GET AND SET FUNCTIONS FOR THE DIMENSIONAL COMPONENTS.


Oh, I thought I should follow the class convention....so I should make them public instead?

thx


Probably not; the constructors and mathematical operators should represent a sufficient interface. Assume they will until your calling code proves a need.
Most vector classes I've used and seen (including my own) make the components public. Definitely make them public for ease of use.

Your get/set function would just return/set the actual component to whatever is passed/requested:
float Vector3::GetX() const { return x; }void  Vector3::SetX(float ix) { x = ix; }...

So, wouldn't you rather just type Vec.x instead of Vec.GetX()?

Unless whenever you set the value of the component you would like to have the vector renormalized or something else, it isn't needed.

Slaru
Quote:Original post by johnnyBravo
Slaru, I'm getting this error:
Quote:
vector.cpp(121) : error C2511: 'Vector Vector::operator *(float,const Vector &)' : overloaded member function not found in 'Vector'
c:\my\new\space\vector.h(2) : see declaration of 'Vector'

for this code:
inline Vector Vector::operator * (float lhs, const Vector& rhs) { //this line is the problem 	return Vector(rhs.x*lhs, rhs.y*lhs, rhs.z*lhs); }

Yes, that is correct. You should be getting that error. The "Vector3 operator * (float lhs, const Vector3& rhs)" function shouldn't be a member function of Vector3, it should be a global function. Your code should be like this:
// No Vector3:: because it isn't a member function of Vector3inline Vector operator * (float lhs, const Vector& rhs) { // Here is the corrected line	return Vector(rhs.x*lhs, rhs.y*lhs, rhs.z*lhs); }

Slaru
Quote:Original post by Zahlman
Probably not; the constructors and mathematical operators should represent a sufficient interface. Assume they will until your calling code proves a need.

I disagree here. Vector classes is one of the few places where making member data (in this case: vector components) public is actually a good idea.
I see, when using the gets, i get an error:
Quote:
error C2662: 'Vector::getX' : cannot convert 'this' pointer from 'const Vector' to 'Vector &'
Conversion loses qualifiers
error C2662: 'Vector::getY' : cannot convert 'this' pointer from 'const Vector' to 'Vector &'
Conversion loses qualifiers
error C2662: 'Vector::getZ' : cannot convert 'this' pointer from 'const Vector' to 'Vector &'
Conversion loses qualifiers

inline Vector operator * (float lhs, const Vector& rhs) { 	return Vector(rhs.getX()*lhs, rhs.getY()*lhs, rhs.getZ()*lhs); }
Quote:Original post by johnnyBravo
I see, when using the gets, i get an error:
Quote:
error C2662: 'Vector::getX' : cannot convert 'this' pointer from 'const Vector' to 'Vector &'
Conversion loses qualifiers
error C2662: 'Vector::getY' : cannot convert 'this' pointer from 'const Vector' to 'Vector &'
Conversion loses qualifiers
error C2662: 'Vector::getZ' : cannot convert 'this' pointer from 'const Vector' to 'Vector &'
Conversion loses qualifiers

*** Source Snippet Removed ***

Why are you using those get functions? Don't use them, the are utterly useless. Just make the components public and do "rhs.x" etc.

I agree with Andrew Russell, the components of a vector class should be made public. Normally, for a class you'd want to hide the actual data, but this is an instance where hiding it and providing get/set functions is useless.

Slaru
The reason that you get those errors (cannot convert 'this' pointer from 'const Vector' to 'Vector &' Conversion loses qualifiers), is because the get functions are not const.

As I suggested - you should not have them in the first place - just make the components public.

As a lesson on how to make your get function const:

class foo{    int bar;public:    int GetBar() const { return bar; }};


Note the use of the word const after the arguments, before the definition.

The const keyword indicates that the function will not change any of the member data. You can't call a non-const function on a const object.

This is because a const object indicates that the data will not be changed. Calling a non-const function on it could cause the data to be changed (whereas, a const function tells the compiler it won't).


See also, my own + and += operators up the page - they show propper use of the const keyword on +, but not on +=.

This topic is closed to new replies.

Advertisement