Your math function syntax

Started by
11 comments, last by WhatEver 22 years, 1 month ago
Do you follow the same syntax as your compiler? All my math functions are in the same order that the compiler calculates. For example: v=v1+v2 My function would look like this: VectorAdd(v, v1, v2); For subtraction: v=v2-v1 My function would look like this: VectorSubtract(v, v2, v1); I find it a lot easier to remember how the function takes its perameters when I lay them out like that. It''s as simple as remembering the compilers syntax. So how many of you use that same reasoning?
Advertisement
Use c++ and your function can look like this:
v = v1 + v2; //vector add
quote:Original post by sjelkjd
Use c++ and your function can look like this:
v = v1 + v2; //vector add


Sure, but if you have large objects with expensive constructors (e.g. large matrices), it can be useful to also pass the return variable as a reference parameter (like when you would use += ).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
You can always use Proxies for deferred evaluation.
That''s object overloading huh? I could never figure out how to do that :/.

How would I go about overloading an object that could do that? Would it be any faster or slower than my way, or is the difference so small it doesn''t matter?
Just to clarify, that's not object overloading, it's function overloading.

The best way to overload an operator such as "+," "[]," etc. for a user-devloped class, is to overload the operator as a method of the class. When overloading a binary operator for a class, you only need to specify one parameter, as the first is always implied to be the current instance of the class, and just the same, unary operators as class members take no parameters as the current instance is implied.

To overload the operators as members, you add the declaration of the overloaded method right in the declaration of your class. The declaration of an overloaded operator is just like the declaration of any other function, only with operators, have the name of the function be the word "operator" followed by the operator you would like to overload.

Example -- Let's say you have a xyzVector that you want to define the "+" operator for so that you'd be able to use the syntax "v1 + v2," where v1 and v2 are instances of the xyzVector class that you have defined.


    class xyzVector{public:     xyzVector( float xInit = 0.0f, float yInit = 0.0f, float zInit = 0.0f )          : x(xInit), y(yInit), z(zInit) {}     xyzVector operator+( xyzVector& VectorToAdd ) const          { return xyzVector( x + VectorToAdd.Getx(), y + VectorToAdd.Gety(), z + VectorToAdd.Getz() ); } // The overloaded declaration     float Getx() const { return x; }     float Gety() const { return y; }     float Getz() const { return z; }     void Setx( float xNew ) { x = xNew; }     void Sety( float yNew ) { y = yNew; }     void Setz( float zNew ) { z = zNew; }protected:     float x, y, z;};  


As you can see, the first parameter is implied to be the instance of the class.

What's great about declaring it as a member function is that you can use polymorphism so that in that case you derive another "type of Vector" from your base vector class, you can redefine the operator with the same parameters for the subclass.

Alternatively, specifically if you are using a class that is already defined you can declare it outside of the class declaration, but you, of course, must provide more information specifying the other parameters. IE, the above case as a global function would be declared as:


  xyzVector operator+ ( xyzVector& MainVector, xyzVector& VectorToAdd ); // As a global function  


The only thing you have to remember is that you must consider whether members of the class are public, private, or protected when defining each operator. You will often times have to be sure to declare the operator function as a friend of the class in order to be able to manipulate the data that you have to (though in this case that was not necessary).


--------------------
Matthew Calabrese
Realtime 3D Orchestra:
Programmer, Composer,
and 3D Artist/Animator
"I can see the music..."

[edited by - Matt Calabrese on March 17, 2002 2:38:22 PM]
http://www.parashift.com/c++-faq-lite/operator-overloading.html
If you have an overloaded operator as a member of a class you run the risk of manipulating the wrong set of data. For instance, you have Vector1 = Vector2+Vector3. If the + operator is overloaded in the class, in order to maintain a consistent state it would have to create an entirely new Vector. The problem with this is that the new vector is destroyed when the function returns. Another way to do it would be to actually change vector2, but that destroys vector2s data. Say you had a,b and c. b = 3, and c = 4. After a=b+c a would equal 7, but if you did the addition by changing b, b would equal 7 too. These are the reasons the format add(storein,vector1,vector2) is the best choice.

Of course, it''s moot for a unary operator that is meant to modify the class it''s defined in anyhow, and any operation that returns an actual number, like a dot product (I always define that as the multiply operator.)
Deyja, you are wrong. There is no possibility of changing the wrong data because operator+ SHOULD return a const Type, and not modify either parameter.
sjelkjd you are not entirely right. Operator+ should return a type, whether it is consant is irrelevant because you can''t change function return types unless they are references to real variables.

Vector3D Vector3D::operator+ (const Vector3D &other) const

is perfectly legal and it''s returnvalue cannot be changed.
Dirk =[Scarab]= Gerrits

This topic is closed to new replies.

Advertisement