# How to get rid of this?

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

## Recommended Posts

Hiya, i was wondering if someone could help me with a design flaw in my library's vector and matrice code. See, like i posted here, i always need to access the x,y,z part of the vector by the letter v, and m for matrices.

Another example would be

ApplyMatrixToVector(&p->ThrustVector.v, &p->JetMatrix.m);



It's kinda annoying to have those v and m letters appended everywhere, but im affraid to modify the code since it work perfectly well.

Also, I know the ApplyMatrixToVector() should be done by overriding the '*' operator but it's already overloaded for something else i think.

Here the source:

vector.h

vector.cpp

Matrix.h

Matrix.cpp

Thx

EDIT: Humm, i know the the header file look a bit strange, it was done that way to compile the library either normally or in dll for multiples languages, just don't mind the #defines (another thing to fix, i need to remove this, but that i dont need help with :) )

Edited by Vortez

##### Share on other sites

Thx for the answer bac, this clear things up a bit. Great explanation btw, i'll look into it.

##### Share on other sites

Ok, so i was speaking with Kathar the other night in the chat about those classes and he did a wonderfull job helping me out. The thing is, im not very used to reference in c++, i always use pointers, but i understand what they are and, i almost understand when to use them but it's tripping me off sometime. Here's the code:

Vector.h

Vector.cpp

Matrix.h

Matrix.cpp

What i don't get is, should't this, for example

CMatrix _EXP_FUNC operator+(CMatrix m1, const CMatrix &m2);

be

CMatrix _EXP_FUNC operator+(const CMatrix &m1, const CMatrix &m2);

???

It mostly confuse me in operator overloading functions... maybe he just missed that, or am i wrong?

##### Share on other sites

What i don't get is, should't this, for example

CMatrix _EXP_FUNC operator+(CMatrix m1, const CMatrix &m2);

be

CMatrix _EXP_FUNC operator+(const CMatrix &m1, const CMatrix &m2);

???

It mostly confuse me in operator overloading functions... maybe he just missed that, or am i wrong?

Follow how the operators are implemented and how the arguments are used. The + operator is implemented as

CMatrix operator+(CMatrix m1, const CMatrix &m2)
{
return m1;
}

where m1.Add(m2) is equivalent to m1 += m2. If you pass m1 as a reference as well, you wouldn't be able to update it and then return it.

You can of course make m1 a const reference as well, but you would then have to create a temporary matrix to hold the result of the addition.

CMatrix operator+(const CMatrix &m1, const CMatrix &m2)
{
CMatrix res = Add(m1, m2); // pseudo-function that returns m1+m2
return res;
}

The non-reference variant uses m1 as that temporary variable.

##### Share on other sites

The non-reference variant uses m1 as that temporary variable.

I see, i think i understand better now.

• 11
• 9
• 11
• 9
• 12