First question, first part. I've had friends in templates without having them defined as inline without any problems, so I doubt it's a rule.
First question, second part. Remember that any member defined in the class declaration is, by default, inline. So:
Code:
template <typename T>
class test {
private:
//data variables
public:
// The following method is inline...
istream& operator >> (istream& stream, test& var){
// Do something and return it...
}
};
Therefore, if you want it to be a friend and inline, define it in the class--like so.
Code:
template <typename T>
class test {
private:
//data variables
public:
// The following method is inline...
friend istream& operator >> (istream& stream, test& var){
// Do something and return it...
}
};
Second question, a string is a type of container. So, you can use iterators. Like so:
Code:
#include <iostream>
#include <string>
int main(int argc, char **argv){
string s("-*abc"); // String
string::iterator it; // Iterator for the string
// Print one character per line...
for (it = s.begin(); it != s.end(); it++)
cout << *it << '\n' << ends;
// Print empty line...
cout << '\n' << ends;
// Set the iterator to the first character...
it = s.begin();
// Move to the third character (two characters ahead)...
it += 2;
// Print the third character...
cout << *it << '\n' << ends;
return 0;
} // End main()
Bookmarks