Jump to content
  • Advertisement
Sign in to follow this  
the_cyberlord

changeing class variables like this: position(20,50,30)??

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

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]

Share this post


Link to post
Share on other sites
Advertisement
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 point
p = point(p.x, p.y, p.z + 5);
q = point(p); // q is created equivalent to p

Share this post


Link to post
Share on other sites
Petewood: I never thought adding two positions together made any sense. On the other hand you could do:

pos += vector( 0, 0, 5 );

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Petewood: I never thought adding two positions together made any sense.
Semantics. 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.

Share this post


Link to post
Share on other sites
Quote:
Original post by the_cyberlord
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
We 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.

Consider the following:

// p is a pre-existing point
p += 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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Quote:
Original post by the_cyberlord
and what is this:
class Foo {
Foo(int a, int b, int c) : x = a, y = b, z = c { }
int x, y, z;
};
That's a constructor, and that's only used when creating the position, like so:

Foo f(a, b, c);
f.x; // == a
f.y; // == b
f.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?
and why is it wrong to do such thing?
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.

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!