![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Programmer
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3
![]() |
To fix your >> operator, try something like the following:
istream& operator>>(istream& in)
{
for(char ch = in.get(); ch != /* whatever you use for a delimiter */ && !in.fail(); ch = in.get()
this.add(ch);
return in;
}That should allow operations to be performed using the "this" pointer created when the >> function was called. |
|
|
|
|
|
#12 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
Twilight the <<, >> operators can't be member functions, as the class parameter needs to be on the right to be called correctly.
If you use your >> function you would have to call it up by doing something like: Stash s; s >> std::cin; ...which dosent make any sense at all. |
|
|
|
|
|
#13 |
|
Programmer
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3
![]() |
Yeah, but when you actually write the class code, you just define the operator. Since the operator itself it just something performed on a class, the this pointer should still work.
|
|
|
|
|
|
#14 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3
![]() |
No. The operator could be a friend function (if access to private stuff is needed; it isnt in this case), but either way the operator is defined globally, so using the this pointer is invalid. Think about it: if you are writing main, and you suddenly use this->something(), does it make any sense?
btw, thanks to Animatronic for pointing out my typo :o |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|