Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: Stonemonkey on March 27, 2007

Title: Won't let me add pointers :(
Post by: Stonemonkey on March 27, 2007
It probably sounds like quite a strange thing to want to do but is it possible to do something like this?

Code: [Select]
p0=v0;
 p2=v2;
 if (v1->y<v0->y) p0=v1;
 if (v2->y<p0->y) p0=v2;
 if (v1->y>v2->y) p2=v1;
 if (v0->y>p2->y) p2=v0;
 p1=(v0+v1+v2)-(p0+p2);   //doesn't like this.

where p_ and v_ are pointers.

Cheers.
Fryer.
Title: Re: Won't let me add pointers :(
Post by: Jim on March 27, 2007
You can do that in C++ with operator overloading.  You need to define the + and - operators on the vector class.  In C you just have to write it out long hand.

Jim
Title: Re: Won't let me add pointers :(
Post by: Stonemonkey on March 27, 2007
Ah, thanks Jim......for sending me into advanced topics in c++ where it starts off with 'when you can stand without toppling over,the real work begins'.
Title: Re: Won't let me add pointers :(
Post by: Jim on March 27, 2007
A quick (not terribly well implemented) example of operator overloading
Code: [Select]
#include <iostream>
using namespace std;

class My3DVector
{
public:
float x,y,z;

My3DVector operator+(My3DVector r)
{
My3DVector w;
w.x = x+r.x;
w.y = y+r.y;
w.z = z+r.z;
return w;
}

My3DVector operator-(My3DVector r)
{
My3DVector w;
w.x = x-r.x;
w.y = y-r.y;
w.z = z-r.z;
return w;
}
};



int main(void)
{
My3DVector a, b, c;

a.x = 1;
a.y = 2;
a.z = 3;

b.x = 10;
b.y = 11;
b.z = 12;

c = a + b;

cout << c.x << "," << c.y << "," << c.z << endl;

return 0;
}
Title: Re: Won't let me add pointers :(
Post by: Stonemonkey on March 27, 2007
Ah right, slight misunderstanding methinks. I'm wanting to actually add and subtract the pointers.

Thanks for that though, that'll be useful too.
Title: Re: Won't let me add pointers :(
Post by: Jim on March 27, 2007
Why would you want to do that?  Can you post a whole thing - with the types of p and q there - and the error message?

Jim
Title: Re: Won't let me add pointers :(
Post by: Stonemonkey on March 27, 2007
They're all vertex pointers, v0,v1,v2 are the 3 verts for a triangle and the code above is to sort them by y for drawing a triangle. The Ifs find the lowest and highest y verts and put them into p0 and p2. The additions and subtractions then find which vert is left and puts that into p1 (supposed to anyway, I did say it would sound strange).

EDIT: y should really be y_2d or something.

error C2110: '+' : cannot add two pointers
Title: Re: Won't let me add pointers :(
Post by: Jim on March 27, 2007
I see what you're trying to do.

If you have
Code: [Select]
int *a, *b;
int diff;
diff = a-b;
Then diff is meaningful.  It's the number of elements between a and b.  Note that the result is an integer, not a pointer.

Code: [Select]
int sum;
int *sptr;
sum = a+b;
sptr = a+b;
Neither of those are meaningful in C or C++.  You have to cast to integers to do it, and then you're on your own as to what the result is supposed to mean.

Why not create p0,p1,p2 pointing to the three vectors, and then just swap pair of them based on the y value?

Jim
Title: Re: Won't let me add pointers :(
Post by: Stonemonkey on March 27, 2007
ok, thanks Jim.