• Create Account

# Question on conversion functions

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1Alex_kir  Members   -  Reputation: 228

Like
0Likes
Like

Posted 29 August 2013 - 01:31 PM

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 ?

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.

### #2_swx_  Members   -  Reputation: 681

Like
1Likes
Like

Posted 29 August 2013 - 01:46 PM

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_, 29 August 2013 - 01:47 PM.

### #3_swx_  Members   -  Reputation: 681

Like
0Likes
Like

Posted 29 August 2013 - 01:49 PM

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

### #4SiCrane  Moderators   -  Reputation: 8536

Like
3Likes
Like

Posted 29 August 2013 - 01:51 PM

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.

### #5_swx_  Members   -  Reputation: 681

Like
1Likes
Like

Posted 29 August 2013 - 01:55 PM

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;
}


### #6Alex_kir  Members   -  Reputation: 228

Like
0Likes
Like

Posted 29 August 2013 - 02:28 PM

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, 29 August 2013 - 02:40 PM.

### #7Paradigm Shifter  Crossbones+   -  Reputation: 4066

Like
1Likes
Like

Posted 29 August 2013 - 02:53 PM

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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #8mk1x86  Members   -  Reputation: 374

Like
0Likes
Like

Posted 30 August 2013 - 01:38 AM

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

### #9LorenzoGatti  Crossbones+   -  Reputation: 2012

Like
0Likes
Like

Posted 30 August 2013 - 02:42 AM

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.

Produci, consuma, crepa

### #10Felix Ungman  Members   -  Reputation: 705

Like
0Likes
Like

Posted 30 August 2013 - 03:06 AM

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.

openwar  - the real-time tactical war-game platform

### #11King Mir  Members   -  Reputation: 1328

Like
0Likes
Like

Posted 30 August 2013 - 02:02 PM

Ideally by having a different type for points and vectors, your the type system will stop you from passing a point as a vector, and a vector as a point. But for that to work you need to be very careful that you always use the correct type when passing to and returning from functions, and avoid providing a way to convert from one to the other.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS