|
CMPT 212
Fall 1997
|
sentence first = oldSentence; // These two lines do the
sentence first(oldSentence); // same thing.
but it does not solve this one:
sentence first;
first = oldSentence; // Initialize an existing instance.
For this we need operator overloading.
const sentence& sentence::operator=(const sentence& s) {
if (this != &s) {
delete [] words;
words = new char[strlen(s.words) + 1];
strcpy(words, s.words);
count = s.count;
}
return *this;
}
sentence a;
sentence b;
...
a = b; // These two lines are
a.operator=(b); // the same.
sentence a;
sentence b;
sentence c;
...
a = b = c;
...
while ((a = readSentence()) != badSentence) {
...
}
ostream& operator<<(ostream& os, const sentence& s) {
os << *(s.words);
return os;
}
sentence a;
cout << a. // These two lines are
operator<<(cout, a); // the same.
Return to lecture notes index |
|
| This page is maintained by simpson@cs.sfu.ca. | Last updated on 11 Oct 1997. |