Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 7th, 2006, 10:23 PM   #11
Twilight
Programmer
 
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3 Twilight is on a distinguished road
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.
Twilight is offline   Reply With Quote
Old May 7th, 2006, 10:34 PM   #12
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
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.
Animatronic is offline   Reply With Quote
Old May 8th, 2006, 4:51 AM   #13
Twilight
Programmer
 
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3 Twilight is on a distinguished road
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.
Twilight is offline   Reply With Quote
Old May 8th, 2006, 5:33 AM   #14
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3 Jimbo is on a distinguished road
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
Jimbo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:37 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC