-
June 25th, 2009, 09:27 AM
#1
Templated class and iostream overloading
Well first I want to say hello to all the users here.
I'm a beginner in C++ and I'm trying to program a vector class for a Math Utility library I'm working on as my first big project written in C++... Well everything is working as expected but I have some questions.
-Info:
All my vector class is in a header file and using the namespace mut (it stands for Math Utility Toolkit, I might change the name but it's ok for the moment) and I'm using a template like this:
Code:
namespace mut{
template <class T=int> //Default vector storage is int
class vector{
.......
}
}
I wanted to add iostream operators to my class and this is what I did:
Code:
//IOSTREAM operators
template <class T>
ostream& operator<<(ostream& os, vector<T> &vec){
int i = 0;
os << "(";
for(i = 0; i < vec.length() - 1; i++){
os << vec[i] << ", ";
}
os << vec[i] << ")" << endl;
return os;
}
template <class T>
istream& operator>>(istream& is, vector<T> &vec){
int i = 0;
for(i = 0; i < vec.length(); i++){
is >> vec[i];
}
return is;
}
So my question is:
I used the template again since these operators are outside the class definition but I don't know if there is a better way or if this is ok...
And a second question is "is there a better way of formatting the output for my vector like so?": (vec[0], vec[1], vec[2]...)
I achieved this format but i don't know if the function can be optimised...
Well thanks in advance!
If you need more info, let me know
BB!
Tags for this Thread
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
|