operator overloading ==

A

A

Hi,
I am writing a "vector" class. I know about the STL but I prefer to
use this as I use this to create a "matrix" class later. I read that
using the inbuilt STL to create a matrix is not the best way to do it.

At the moment, the class "vec" looks like this.
template <class T>
class vec {
private:
int length;
T *v;
public:
vec();
explicit vec(int n);
vec(const T &a, int n);
vec(const T *a, int n);
vec(const vec &cpy);
vec & operator=(const vec &equate);
vec & operator=(const T &a);
inline T & operator[](const int i);
inline const T & operator[](const int i) const;
inline int size() const;
~vec();
};

Now, I want to overload the operator == in this way. It should accept
a value (type T) say "val", and check through all the entries of this
vector say "v1" and return another vector of type bool, which has
"false" where v1 != val and "true" where v1 == val. I managed to write
it this way...

member function:
vec<bool> operator==(const T &a) const;

the code definition is:
template <class T>
vec<bool> operator==(const T &a) const {
vec<bool> tmp(false,length);
for(int i=0; i<length; i++) {
if(v == a) {
tmp = true;
}
}
return tmp;
}

This works great, but I would like to return the reference instead of
the whole vector and would like to know how to do it. I am unable to
make use of the "this" pointer as I am returning a "local vector". I
would appreciate any help with this. thank you!
 
K

Keith Thompson

A said:
I am writing a "vector" class. I know about the STL but I prefer to
use this as I use this to create a "matrix" class later. I read that
using the inbuilt STL to create a matrix is not the best way to do it.
[...]

This is comp.lang.c. You want comp.lang.c++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top