Jump to content
• Advertisement

Public Group

# Question on conversion functions

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

Hello guys and thanks to all of you in advance for the help.

So for the question:

I have 2 simple classes

class Vector2D{

float X,Y;

}

class Point2D{

float X,Y;

}


as simple as it gets.

Now I want to be able to perform basic math on them like

Vector2D vec;
Position2D EntityPosition1;
Position2D EntityPosition2;

vec = EntityPosition1 - EntityPosition2;


Now I already have an operator overload for the - but when I try to do it says that there is no possible conversion from Point2D to Vector2D, fare nuff.

Simple googling pointed me to the conversion functions but while I was browsing I was left with the impression that conversion functions are actually a bad thing, is that true or did I just go too shallow on the googling :o ?

Also about the syntax itself.

So I added this peace of code into my Vector2D class header

operator Point2D();


so far so good but when I go into the Vector2D.cpp file and simply write

operator Point2D(){

}


without adding anything else I immidatly get an error message saying "conversion function must be a nonstatic member function", that doesnt really make sence considering its a member function and its not static ( atleast thats my understanding of the syntax while looking at the examples on google but I guess I am clearly wrong )

I dont understand the syntax of how this things works and quite frankly documentiation on google doesnt make it easyer as well, I am not sure how the syntax even works or if I should be using conversion functions to begin with.

Again thanks in advance for the help guys and sorry for the long post on this simple mather, any help appriciated <3

P.S: I've been loving the Game developing article project so much its really a great way to learn.

#### Share this post

##### Share on other sites
Advertisement

Avoid conversion operators and find alternative solutions like:

Vector2D operator-(const Position2D &lh, const Position2D &rh)
{
return Vector2D(lh.x - rh.x, lh.y - rh.y);
}

Edited by _swx_

#### Share this post

##### Share on other sites

I don't recommend having different types for points and vectors though... it will make your code much messier with lots of conversions.

#### Share this post

##### Share on other sites
Like all externally defined member functions, you need to qualify the conversion operator definition with the class name.
Vector2D::operator Point2D() {
// whatever
}

Note that since you have control over both Point2D and Vector2D a better solution might be giving Point2D a constructor that takes a Vector2D or const reference to Vector2D and vice versa. Consider making the constructors explicit and if using C++11 making the conversion operators explicit.

#### Share this post

##### Share on other sites
struct Vector2D{
float x, y;
};

struct Point2D{
float x, y;

operator Vector2D() const {
Vector2D r = { x, y };
return r;
}
};

Point2D operator-(const Point2D &lh, const Point2D &rh)
{
Point2D r = { lh.x - rh.x, lh.y - rh.y };
return r;
}

int main()
{
Point2D x = { 1, 1 };
Point2D y = { 2, 2 };
Vector2D z = x - y;
}


#### Share this post

##### Share on other sites

Like all externally defined member functions, you need to qualify the conversion operator definition with the class name.

Yeah I feel emberessed because of the silly mistake

I don't recommend having different types for points and vectors though

Yeah I am aware that they can be represented as 1 class but my mindset was:

Well vectors represent direction and length but they dont know where they are at, aka they dont contain information about their position

Points on the other hand are exacly the opposite they represent position but have neither length nor direction

And yea while the fundemantle ideas behind the 2 are completly diffrent, their structure ( representation w/e) is exacly the same BUT because of the core ideas being opposite to each other, it will actually be confusing for myself to have them be the same class thats why I chose to seperate them ( and yea I am already feeling the negatives of it )

P.S: Thanks to both of you for all your help guys  ;3 <3 the examples were really helpful as well ^^

Edited by Alex_kir

#### Share this post

##### Share on other sites

Subtracting 2 points should return a vector (vector between the 2 points).

Adding 2 points should not be allowed, it doesn't make any sense geometrically.

Adding a vector to a point is allowed it should return a point.

#### Share this post

##### Share on other sites

If I understand you correctly, all you want to do is to distinguish between direction vectors and positions for sake of better readability of your code. Since points and vectors are essentially the same I propose to write one vector class and add a typedef for points. That way everything works out of the box but you can still use "Point2D" so the reader knows you refer to a point rather than a direction vector.

While you're at it you should consider writing a template class so you can have other than float vectors.

template<class T> vector2d

#### Share this post

##### Share on other sites

Separate point and vector types are mathematically correct and the separation should make most code easier to read and write. After all, operations are very different.

#### Share this post

##### Share on other sites

Conceptually you have points representing absolute positions and vectors representing relative displacement. On the other hand, given that position is always uniquely determined by a certain displacement from the origin, and that the x and y coordinates for a position and its corresponding displacement from the origin are the same, it's not too far fetched to use the same kind of object to represent both concepts. Mathematicians do that all the time.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Now

• 12
• 18
• 29
• 11
• 24
• Advertisement
×

## Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!