|
-
Nov 2nd, 2004, 07:13 PM
#1
Operator overloading?
Do the functions have to be in a .h file? I have just tried to add the class definition in a .h file and then the functions in the .cpp file and I can't get it to work. Isn't it supposed to work?
ØØ
Last edited by NoteMe; Nov 2nd, 2004 at 08:07 PM.
-
Nov 2nd, 2004, 07:24 PM
#2
Just for the record. Here is the code, maybe there is something else wrong. I have only done this once before, and that is 2 years ago now, so please don't beat me to deth if it is obvious....
.h file
Code:
#ifndef _CVECTOR3_H
#define _CVECTOR3_H
// This is our basic 3D point/vector class
class CVector3{
public:
float x;
float y;
float z;
// A default constructor
CVector3();
// This is our constructor that allows us to initialize our data upon creating an instance
CVector3(float X, float Y, float Z);
// Here we overload the + operator so we can add vectors together
CVector3 operator+(CVector3 vVector);
// Here we overload the - operator so we can subtract vectors
CVector3 operator-(CVector3 vVector);
// Here we overload the * operator so we can multiply by scalars
CVector3 operator*(float num);
// Here we overload the / operator so we can divide by a scalar
CVector3 operator/(float num);
};
#endif
.cpp file
Code:
#include "cvector3.h"
// A default constructor
CVector3::CVector3() {
}
// This is our constructor that allows us to initialize our data upon creating an instance
CVector3::CVector3(float X, float Y, float Z) {
x = X; y = Y; z = Z;
}
// Here we overload the + operator so we can add vectors together
CVector3 CVector3::CVector3 operator+(CVector3 vVector){
// Return the added vectors result.
return CVector3(vVector.x + x, vVector.y + y, vVector.z + z);
}
// Here we overload the - operator so we can subtract vectors
CVector3 CVector3::CVector3 operator-(CVector3 vVector){
// Return the subtracted vectors result
return CVector3(x - vVector.x, y - vVector.y, z - vVector.z);
}
// Here we overload the * operator so we can multiply by scalars
CVector3 CVector3::CVector3 operator*(float num){
// Return the scaled vector
return CVector3(x * num, y * num, z * num);
}
// Here we overload the / operator so we can divide by a scalar
CVector3 CVector3::CVector3 operator/(float num){
// Return the scale vector
return CVector3(x / num, y / num, z / num);
}
and the errors that I am getting. It looks like it can't find the member variables, and the two last warning kind of concerns me...
-
Nov 2nd, 2004, 08:07 PM
#3
-
Nov 3rd, 2004, 04:25 AM
#4
Pass the arguments to the operators as const references. Use an initializer list in the argument constructor. And initialize the components to 0 in the default constructor, having some constructors leave the object in an undefined state and others not is usually not a good idea.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 3rd, 2004, 06:11 AM
#5
Originally posted by CornedBee
Pass the arguments to the operators as const references. Use an initializer list in the argument constructor. And initialize the components to 0 in the default constructor, having some constructors leave the object in an undefined state and others not is usually not a good idea.
I changed all the parameters to const refs after I posted this. ANd I am never using the default constructor, so I don't see the point, but maybe I will use the class an other time too, so why not.
Using an initialisation list was not in my mind? Why do I have to do it in this example. Is it faster, or is there something else I have forgotten about this?
-
Nov 3rd, 2004, 08:29 AM
#6
Leaving bad or unpredictable code "because I don't use it anyway" is a wonderful source for future bugs.
Why do I have to do it in this example. Is it faster, or is there something else I have forgotten about this?
There are two reasons why you should do it in this example. First, to get into the habit. Second, because it may allow the compiler to do optimizations it can not or will not use otherwise.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 3rd, 2004, 10:19 AM
#7
Originally posted by CornedBee
Leaving bad or unpredictable code "because I don't use it anyway" is a wonderful source for future bugs.
Yeah thats why I wrote the "but maybe I will use the class an other time too, so why not. " part....
Originally posted by CornedBee
There are two reasons why you should do it in this example. First, to get into the habit. Second, because it may allow the compiler to do optimizations it can not or will not use otherwise.
I know I have a bad habbit of leaving it out. Probably because I never remember when I can do it, and when not to. Well I will do it now, and try to remember it next time. Thanks for very inteligent answers...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|