Skip to content

Commit

Permalink
Added ability to cast to a different size Vector. Also added Vector::…
Browse files Browse the repository at this point in the history
…size.
  • Loading branch information
robosam2003 committed Jul 18, 2022
1 parent 37aeeb4 commit 601b3e4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 12 additions & 2 deletions Vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Vector {
*/
Vector<T, s>& operator*=(double scalar);


/**
* @brief operator += : add equals two vectors of the same size together
* @param v
Expand Down Expand Up @@ -114,9 +115,18 @@ class Vector {
*/
Vector<T, s>operator-(Vector<T, s> v);

/**
* @return the size of the Vector
*/
unsigned int size();

// Casting operators
template<typename D>
explicit operator Vector<D, s>() const;
/**
* @brief C-style cast to a different vector type, or size, or both!
* @return
*/
template<typename D, unsigned int newSize>
explicit operator Vector<D, newSize>() const;

// TODO: iterators? begin functions for range-for?

Expand Down
14 changes: 10 additions & 4 deletions Vector.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,18 @@ Vector<T, s> Vector<T, s>::operator-(Vector<T, s> v) {
return rv;
}

template<typename T, unsigned int s>
unsigned int Vector<T, s>::size() {
return s;
}

// Casting operators
template<typename T, unsigned int s>
template<class D>
Vector<T, s>::operator Vector<D, s>() const {
Vector<D, s> rv;
for (unsigned int i=0; i<s; i++) {
template<class D, unsigned int newSize>
Vector<T, s>::operator Vector<D, newSize>() const {
unsigned int min = (s <= newSize) ? s : newSize;
Vector<D, newSize> rv;
for (unsigned int i=0; i<min; i++) {
rv[i] = static_cast<D>(elem[i]);
}
return rv;
Expand Down

0 comments on commit 601b3e4

Please sign in to comment.