c ++ – Class for storing and manipulating Cartesian coordinates and vectors

I created a C ++ class to manipulate 3D vectors in Cartesian coordinates.

However, the performance of my class is much slower (about 2.5x) than the use of something like: double p [3] and then running for loops for additions, subtractions, etc.

My specific concerns are:

  • Is there anything fundamentally wrong that I do that causes this slowdown?
  • Is there a way to achieve what I try, but without slowing down as much?
  • Is it just a fundamental limitation of classes and operator overload that I have to live in return for convenience?

Here is the class:

#ifndef COORD_H
#define COORD_H


#understand 
#understand 
#understand 
#understand 
#understand 
#understand 
#understand 
#understand 
#include "math.h"


// Useful reference: http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html


/ *!  brief Class to store and manipulate points or position vectors in 3D, in Cartesian coordinates. * /
vect3D class
{
public:

double x = 0.0, y = 0.0, z = 0.0;
double tol = 1.0e-15;


// builders
vect3D () {};
vect3D (std :: initializer_list RHS) {x = * RHS.begin (); y = * (RHS.begin () + 1); z = * (RHS.begin () + 2); };
vect3D (std :: vector & RHS) {x = RHS[0]; y = RHS[1]; z = RHS[2]; };
vect3D (double * RHS) {x = RHS[0]; y = RHS[1]; z = RHS[2]; };


// assignment
vect3D & operator = (const vect3D & RHS) {x = RHS.x; y = RHS.y; z = RHS.z; return * this; };


// Addition and subtraction
vect3D & operator + = (const vect3D & RHS) {x + = RHS.x; y + = RHS.y; z + = RHS.z; return * this; };
vect3D & operator - = (const vect3D & RHS) {x - = RHS.x; y - = RHS.y; z - = RHS.z; return * this; };

vect3D & operator + = (double const and RHS) {x + = RHS; y + = RHS; z + = RHS; return * this; };
vect3D & operator - = (double const and RHS) {x - = RHS; y - = RHS; z - = RHS; return * this; };

operator vect3D + (const vect3D & RHS) {returns vect3D (* this) + = RHS; };
operator vect3D - (const vect3D & RHS) {returns vect3D (* this) - = RHS; };

Operator vect3D + (double const & RHS) {returns vect3D (* this) + = RHS; };
Operator vect3D - (double const & RHS) {returns vect3D (* this) - = RHS; };


// Product and scalar division
vect3D & operator * = (double const and RHS) {x * = RHS; y * = RHS; z * = RHS; return * this; };
vect3D & operator / = (double const and RHS) {x / = RHS; y / = RHS; z / = RHS; return * this; };

Operator vect3D * (double const & RHS) {returns vect3D (* this) * = RHS; };
Operator vect3D / (double const & RHS) {return vect3D (* this) / = RHS; };

friend vect3D operator * (double c, vect3D & vec) {return vec * c; };
friend operator vect3D / (double c, vect3D & vec) {return vec / c; };


// Comparisons
operator bool == (const vect3D & RHS) {return ((x - RHS.x <x * tol) && (y - RHS.y <y * tol) && (z - RHS.z < z*tol)); };
    bool operator!=(const vect3D &RHS) { return !(*this == RHS); };

    bool operator>= (const vect3D & RHS) {return ((x> = RHS.x) && (y> = RHS.y) && (z> = RHS.z)); };
Boolean operator <= (const vect3D & RHS) {return ((x <= RHS.x) && (y <= RHS.y) && (z <= RHS.z)); };
    bool operator>(const vect3D & RHS) {return! (* this <= RHS); };
bool operator<(const vect3D &RHS) { return !(*this >= RHS); };


// Euclidean norm
double norm2 () {return std :: sqrt (std :: pow (x, 2) + std :: pow (y, 2) + std :: pow (z, 2)); };
friend double standard2 (const and vect3D) {return std :: sqrt (std :: pow (a.x, 2) + std :: pow (a.y, 2) + std :: pow (a.z, 2)); };


// Scalar product
double point friend (vect3D const & a, vect3D const & b) {return a.x * b.x + a.y * b.y + a.z * b.z; };

// Cross product
friend vect3D traverse (vect3D const & a, vect3D const & b) {return {(a.y * b.z-a.z * b.y), (a.z * b.x-a.x * b.z), (a.x * b.y-a.y * b.x)}; };


// Print to broadcast
friend std :: ostream & operator << (std :: ostream & stream, vect3D const & p) {return flow << "(" << px << "," << py << "," << pz << ")" << std :: flush; };


// Function to explicitly return coordinates as an array of duplicates, if necessary
cancel DoubleArray (double * v) {v[0] = x; v[1] = y; v[2] = z; return; };


// To access the coordinates using the notation in square brackets, for convenience
std :: vector p = std :: vector {& X Y Z};
double operator [] (int ii) const {return * (p[ii])};

};

#end if

Thank you!