Help, can't overload operator>>()

Help, can't overload operator>>()

Post by C & C Helc » Thu, 04 Dec 1997 04:00:00



I want to overload the extractor operator to accept a const char pointer
like this:

istream& operator>>(istream& is, const char* ccp) {
    // Code that reads in the next string, compares it against ccp, sets
ios to fail state
    // if there is not a match.
   return is;

Quote:}

When I try to use this code as in:

   cin >> "Value: ";

operator>>(istream&, char*) is called instead of my routine and
unpleasent things happen. The only way I've been able to make this work
is with casts like this:

    cin >> static_cast<const char*>("Value: ");

This is too ugly. What am I doing wrong?

BTW. What to people think about overloading operator>>() to take const
arguements? It seems kinda wierd but it lets me write parsers much
easier:

    int first, second;
    if (cin >> "Tuple:" >> '{' >> first >> ',' >> second >> '}')
        // Blah blah blah

Regards,
Christopher Helck


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Help, can't overload operator>>()

Post by Valentin Bonnar » Fri, 05 Dec 1997 04:00:00



Quote:

> I want to overload the extractor operator to accept a const char pointer
> like this:

> istream& operator>>(istream& is, const char* ccp) {

Two comments:
- it's a very weird thing; avoid it
- your compiler is too old

Quote:> When I try to use this code as in:

>    cin >> "Value: ";

> operator>>(istream&, char*) is called instead of my routine and
> unpleasent things happen.

Very unpleasent. String litteral used to be of type char[].

Quote:> The only way I've been able to make this work
> is with casts like this:

>     cin >> static_cast<const char*>("Value: ");

> This is too ugly. What am I doing wrong?

You shouldn't overlaod iostream like that, it's a bad
idea. Overload on a type completly different from cv char*,
like my_struct_type (here called match_string):

cin >> match_string ("Tuple: ") >> t;

--


info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Help, can't overload operator>>()

Post by Robert O'Dow » Sat, 06 Dec 1997 04:00:00



Quote:

> I want to overload the extractor operator to accept a const char pointer
> like this:

> istream& operator>>(istream& is, const char* ccp) {
>     // Code that reads in the next string, compares it against ccp, sets
> ios to fail state
>     // if there is not a match.
>    return is;
> }

> When I try to use this code as in:

>    cin >> "Value: ";

> operator>>(istream&, char*) is called instead of my routine and
> unpleasent things happen. The only way I've been able to make this work
> is with casts like this:

>     cin >> static_cast<const char*>("Value: ");

> This is too ugly. What am I doing wrong?

> BTW. What to people think about overloading operator>>() to take const
> arguements? It seems kinda wierd but it lets me write parsers much
> easier:

>     int first, second;
>     if (cin >> "Tuple:" >> '{' >> first >> ',' >> second >> '}')
>         // Blah blah blah

Gack!!!

If memory serves, a string literal (like "Value: ") is able to be used
as an lvalue.  An lvalue can be assigned.  Stretching my memory further,
it has type "array of n const char" where n is the strlen(literal)+1.
So, when given a choice between

istream &operator>>(istream &, const char *)

and

istream &operator>>(istream &, char *)

the compiler should chose the second when given the code cin >> "Hello";
The second overloaded forma always exists, so ....

IMHO, defining an extractor that does not modify it's arguments
is a contradiction.  You are really testing the stream, which suggests
to me you should used istream manipulators.  The following (untested)
code should give you a start

#include <iostream.h>
#include <iomanip.h>

istream &gettoken(istream &s, const char *token)
{
   // whatever you are doing in your const char * extractor
 return s;

Quote:}

imanip<const char *> gettoken(const char *token)
{
 return imanip<int>(gettoken, token);

Quote:}

main()
{
 cin >> gettoken("Tuple:{") >> x >> gettoken(",") >> y
     >> gettoken("}");

Quote:}

- <Automagically appended trailer>
Robert O'Dowd                   Phone   : +61 8 9553 3618
DSTO Bldg A51,                  Fax     : +61 8 9553 3577

Rockingham, Western Australia, 6958

Disclaimer: Opinions above are MINE and may be worth what you paid for
them.


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Help, can't overload operator>>()

Post by David Harm » Sat, 06 Dec 1997 04:00:00




Quote:>BTW. What to people think about overloading operator>>() to take const
>arguements? It seems kinda wierd but it lets me write parsers much
>easier:

>    int first, second;
>    if (cin >> "Tuple:" >> '{' >> first >> ',' >> second >> '}')
>        // Blah blah blah

I think that if you ever got input that failed to match your
expectations you would find yourself in a hard to recover from mess.
Sorry.


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

1. Resizing a CListBox>>>>>>>>>>>>>>>>>>>>>>>>>>>

I have created a CListBox on a view...Now how can I make the listbox always
cover the entire client area of the view, even on resizing.

At the moment im overriding OnSize:-

void CMyView::OnSize(UINT nType, int cx, int cy)
{
        myListBox.MoveWindow(0,0,cx,xy,TRUE);

It doesnt do the trick. Help!!

--
================================================================================
Mr Abtar Bangar          | EMAIL ADDRESS:       |

Southbank University     |                      |  something about EVERYTHING
103 Borough Road         |                      |      but definately not    
LONDON SE1 0AA           |                      |  EVERYTHING about something'
United Kingdom.          |                      |
================================================================================

2. 2181 EZ-ICE vs. 2111 EZ-ICE

3. " INNOVATION INFOS + IMAGING " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

4. OWL and threads

5. Any reason C++ couldn't adopt Java's '>>>' operator?

6. Controlling type size of figure captions

7. HELP >>>>>>>>>>>>> WINAPI <<<<<<<<<<<<<<<<<

8. install orcale 9i in red hat 9

9. safe istream operator>>()

10. Precedence of operator>>()?

11. Setting fail state in istream::operator>>()

12. overloading operator->()

13. example: overloading operator->()