changeing class variables like this: position(20,50,30)??
How do make a class for a 3D position (with an x, y, z value) and make it so, that your code could look like this:
position pos;
pos(x,y,z+5);
// Leave the x, y variables and add five to the Z variable
the class itself should look like this:
class position {
float x, y, z; }
Thanks in forward,
Adriaan
[Edited by - the_cyberlord on October 23, 2004 4:35:00 AM]
Overload operator(). It's a bad idea, though, because it's an unfamiliar notation and will have interesting interactions with other operators due to precedence. Implement a copy constructor and assignment operator instead, which lets you use the following (unambiguous) notations:
// p is a pre-existing pointp = point(p.x, p.y, p.z + 5);q = point(p); // q is created equivalent to p
Or, alternatively you could implement a move() member function and call:
pos.move( 0, 0, 5 );
pos.move( 0, 0, 5 );
Petewood: I never thought adding two positions together made any sense. On the other hand you could do:
pos += vector( 0, 0, 5 );
pos += vector( 0, 0, 5 );
you all don't understand what I want I think...
The pos.move function comes close, but I just want a way to do the same without function call, just an easier way to change the variables inside the class
The pos.move function comes close, but I just want a way to do the same without function call, just an easier way to change the variables inside the class
Quote:Original post by ToohrVykSemantics. What is a position (in this case/context)? It's the point defined by a vector displacement from the origin. Two vector displacements can be added together, therefore the operation is semantically well-formed.
Petewood: I never thought adding two positions together made any sense.
Quote:Original post by the_cyberlordWe perfectly understand what you want. We're just pointing out that doing that is wrong. Furthermore, it's no more efficient than calling a move(x, y, z) method. It's also restrictive, because proper implementation of addition and assignment operators allows you to use standard algebraic constructs, which makes your code more comprehensible.
you all don't understand what I want I think...
The pos.move function comes close, but I just want a way to do the same without function call, just an easier way to change the variables inside the class
Consider the following:
// p is a pre-existing pointp += q + point(0, 0, -5) - r;
Being able to implement arbitrary algebras is a more robust design, and a more comprehensible one. You can even use a template expansion to optimize performance, if you wish.
and what is this:
class Foo {
Foo(int a, int b, int c) : x = a, y = b, z = c { }
int x, y, z;
};
could that be the thing I need?
and why is it wrong to do such thing?
class Foo {
Foo(int a, int b, int c) : x = a, y = b, z = c { }
int x, y, z;
};
could that be the thing I need?
and why is it wrong to do such thing?
Quote:Original post by the_cyberlordThat's a constructor, and that's only used when creating the position, like so:
and what is this:
class Foo {
Foo(int a, int b, int c) : x = a, y = b, z = c { }
int x, y, z;
};
Foo f(a, b, c);f.x; // == af.y; // == bf.z; // == c
It's also inaccurate. Initializers are not complete expressions; they're constructor-type invocations themselves. The correct implementation is as follows:
class Foo{ Foo(int a, int b, int c) : x(a), y(b), z(c) {} int a, b, c;};
Quote:could that be the thing I need?What you're asking for (modification after creation) requires other methods. Your exact request requires an abuse of the function call operator, and results in code that isn't meaningful on inspection.
and why is it wrong to do such thing?
Simply, I'm not going to tell you how to do it. Code correctness is important. Function-call syntax that modifies the function object is not correct.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement