patterncppModerate
C++ 3D Vector Implementation
Viewed 0 times
implementationvectorstackoverflow
Problem
I have been learning C++ now for 2 months and this week I started reading a book on 3D graphics. I like coding whatever mathematical stuff I learn so I can understand it better, so when I learnt about Vectors, I decided to write a class on it.
I'd be grateful for any suggestions on my code, be it style-wise, performance-wise, or anything at all.
Some considerations I took when writing this code were:
I use inline functions because I heard that the C++ compiler, while smart, will not be able to inline everything that I want to be inlined automatically, even if I give hints.
I use commenting style that takes a lot of extra space. For me personally, it aids me in reading and documenting my code step by step.
I heard use of 'friend' operator is discouraged, but I seem to like using it. It allows me to code functions that, while could work as methods (e.g. vector.CrossProduct(otherVector)) sound better as functions CrossProduct(vector1, vector2) in my opinion.
I don't comment the implementation code. It seems too trivial to comment, I wonder if you think this is the case too?
```
//
//* Vector.h
//*
//* Just Another Vector Implementation (JAVI)
//
#ifndef __VECTOR_H__
#define __VECTOR_H__
#include "Math.h"
#include
class Vector
{
public:
//
//* Constructors
//
// Default Constructor
//------------------------------------------------------------------
// Sets the x, y and z components of this Vector to zero.
//------------------------------------------------------------------
Vector ();
//------------------------------------------------------------------
// Component Constructor
//------------------------------------------------------------------
I'd be grateful for any suggestions on my code, be it style-wise, performance-wise, or anything at all.
Some considerations I took when writing this code were:
I use inline functions because I heard that the C++ compiler, while smart, will not be able to inline everything that I want to be inlined automatically, even if I give hints.
I use commenting style that takes a lot of extra space. For me personally, it aids me in reading and documenting my code step by step.
I heard use of 'friend' operator is discouraged, but I seem to like using it. It allows me to code functions that, while could work as methods (e.g. vector.CrossProduct(otherVector)) sound better as functions CrossProduct(vector1, vector2) in my opinion.
I don't comment the implementation code. It seems too trivial to comment, I wonder if you think this is the case too?
```
//
//* Vector.h
//*
//* Just Another Vector Implementation (JAVI)
//
#ifndef __VECTOR_H__
#define __VECTOR_H__
#include "Math.h"
#include
class Vector
{
public:
//
//* Constructors
//
// Default Constructor
//------------------------------------------------------------------
// Sets the x, y and z components of this Vector to zero.
//------------------------------------------------------------------
Vector ();
//------------------------------------------------------------------
// Component Constructor
//------------------------------------------------------------------
Solution
-
Comments
Honestly, too many of them.
-
Object-orientation
I don't see why vector components are made private. A client can freely and independently modify them via
-
Math
I am quite surprised by the presence of
On the other hand, dot product would very naturally be
There is no implementation of
-
Implementation details
It is strongly recommended to express
Similarly, other operators with tightly bound semantics also should not be independent. It is usually recommended to express
Comments
Honestly, too many of them.
-
Object-orientation
I don't see why vector components are made private. A client can freely and independently modify them via
Set method. There's no internal state to maintain, no invariant to protect. I recommend to make them public and eliminate Set(), X(), Y(), Z() methods altogether.-
Math
I am quite surprised by the presence of
operator/(float, Vector). There is no immediately obvious value in it. Mathematically such operation makes no sense, and dividing a scalar by a vector shall be flagged as error ASAP.On the other hand, dot product would very naturally be
float operator*(Vector&, Vector&)There is no implementation of
AreEquals and EqualsZero methods. In any case, I'd expect IsZero method to compare a norm rather than individual components. It would actually be nice to abstract a norm calculation (right now the client is forced to Euclidean distance).-
Implementation details
It is strongly recommended to express
operator != in terms of operator ==. Otherwise, you face with a double maintenance problem, and the reader should do extra work to make sure that the semantics of comparisons is correct.Similarly, other operators with tightly bound semantics also should not be independent. It is usually recommended to express
operator+ in terms of operator+=, etc. For more details, see a Canonical Implementation section in C++ reference.Code Snippets
float operator*(Vector&, Vector&)Context
StackExchange Code Review Q#62764, answer score: 16
Revisions (0)
No revisions yet.