Jump to content
  • Advertisement
Sign in to follow this  
Black Knight

x y z or float p[3]

This topic is 4615 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

Not really. I've had them public in my classes, Microsoft has them public in their classes so it must be alright. Then again it does break the law of encapsulation, so Object-Oriented purists would say the data members *have* to be private.

I'd say its down to coding style, I think the public data members are more elegant ( theVec.x instead of theVec.X() ), and it's unlikely a vector class would ever change after its completed. Then again if it were to change...

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Black Knight
another problem when overloading [] is when I have a array of vectors lets say

Vector_3 *pVertices = new Vector_3[1000];

not If i want to reach the 500 vertex

pVertices[500] but this will call the overloaded [] right??What will happen?


No, it would call the array as normal. To call the overload you would do something like:

Vector_3 *pVertices = new Vector_3[1000];

pVertices[500][0] = 2.30f;

The array acts like a 2d array, the [500] references the vector to edit, the [0] references the x in the 500th vector. You could also do this:

pVertices[500].x = 2.30f;
pVertices[500] = Vector_3( 2.30f, 0.00f, 1.00f );

Assuming a constructor existed that took 3 floats.

Share this post


Link to post
Share on other sites
wrt to union vs operator, I personally prefer to take the best of both worlds
class Vector_3
{
public:
union
{
struct
{
float x, y, z;
};
float p[3];
};
float&operator [](int index)
{
return p[index];
}
};

Quote:
Original post by Black Knight
Doh yea unions.
Well unions share the same memory as far as i know.So Im not wasting any memory for it its the same as 3 floats?
And whats the purpose of the struct inside the class cant i just have :

class Vec3
{
public:
union{
float x,y,z;
float p[3];
};
};

This seems to work.


The inner-struct is there to prevent the union from making x, y and z a single variable. Without it, x=y=z=p[0], which isn't what you want.

Share this post


Link to post
Share on other sites
m:\OddProject\Source\MogsIsland\Maths\Vector3.inl(18): error C2440: 'return' : cannot convert from 'const Real' to 'Real &'

Throws my const correctness out.

Share this post


Link to post
Share on other sites
I tried to make some template funcs then i removed them but I get errors now like this
C:\Program Files\Microsoft Visual Studio 8\VC\include\xutility(572) : error C2039: 'iterator_category' : is not a member of 'Vector_4'
d:\st projects (source)\stlibs\stengine\math\Vector.h(128) : see declaration of 'Vector_4'
.\Drawable.cpp(142) : see reference to class template instantiation 'std::iterator_traits<_Iter>' being compiled
with
[
_Iter=Vector_4
]

I dunno how to fix them.Any idea?

Share this post


Link to post
Share on other sites
Quote:
Original post by TheOddMan
I'd say its down to coding style, I think the public data members are more elegant ( theVec.x instead of theVec.X() ), and it's unlikely a vector class would ever change after its completed. Then again if it were to change...


My view is that 'theVec.X()' generally (a) adds extra useless complexity and (b) is effectively lying. Real classes can perform more useful things, anyway. This sort of function is just adding a useless 'hook' for invariant-maintaining code and then (in practice) never filling it in.

Share this post


Link to post
Share on other sites
Ok another useless question :)
lets say i have this

Vector_3 normalize(const Vector_3 *pNormal)
{
float mag = pNormal->magnitude();
Vector_3 vNormal = Vector_3(pNormal->x / mag,pNormal->y / mag,pNormal->z / mag);
return vNormal;
}


Now what happens if I call it from my app like this??

vector3 = normalize(&(vector1 - vector2));

It does not give a compile error but what address does it send to the function??
Sorry for the Qs :]


Share this post


Link to post
Share on other sites
Don't do that, is the short answer. Instead, make them friend functions, and pass by reference:


class Vector3
{
// friend functions can access the members of the classes they're
// declared as friends in
inline friend float Magnitude(const Vector3& v)
{
return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
}

inline friend Vector3 Normalize(const Vector3& v)
{
float m = Magnitude(v);
return v / m;
}

// likewise for CrossProduct:
inline friend Vector3 CrossProduct(const Vector3& lhs, const Vector3& rhs)
{
return Vector3( lhs.y * rhs.z - lhs.z * rhs.y,
lhs.z * rhs.x - lhs.x * rhs.z,
lhs.x * rhs.y - lhs.y * rhs.x );
}
};


Edit: I should probably mention that you use them like ordinary functions (well, they are ordinary functions, just with special permissions, really). Like so:


int main()
{
Vector3 v = CrossProduct(v1, v2);
}
You don't have to do any Vector3::CrossProduct(...) stuff at all. Easy.

Share this post


Link to post
Share on other sites
Just use references:
Vector_3 normalize(const Vector_3& normal)
{
float invMag = 1.0f/normal.magnitude();
return Vector3(normal.x*invMag, normal.y*invMag, normal.z*invMag);
}
Or something similar.

Share this post


Link to post
Share on other sites
When you call (vector1 - vector2) a temporary instance of class Vector_3 is created, and the function gets the address of this temporary. Now passing the address of a temporary is not the best idea, as you might imagine...

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!