• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

overloading >> operator

istream &operator>>(istream &In,StrC &Copy)
{
      char* Buffer;
      In.getline (Buffer,'\n');
      delete [] Copy.TextBuffer;
      Copy.TextBuffer = new char[strlen(Buffer)+1];
      strcpy(Copy.TextBuffer,Buffer);
      delete [] Buffer;
      return In;
}

You can see from that example what I am trying to do, however, it does not work.  It seems you can't use
a char* inside of a getline like this.  What can I do to insure my Copy.TextBuffer not only does not go outside
of its bounds, but enlarges to accept more than its current bounds when needed?
0
List244
Asked:
List244
  • 4
  • 3
  • 2
1 Solution
 
jkrCommented:
>>It seems you can't use a char* inside of a getline like this.

Not an *unitialized* one - use either

istream &operator>>(istream &In,StrC &Copy)
{
     char* Buffer = new char[MAX_SIZE];
     In.getline (Buffer,'\n');
     delete [] Copy.TextBuffer;
     Copy.TextBuffer = new char[strlen(Buffer)+1];
     strcpy(Copy.TextBuffer,Buffer);
     delete [] Buffer;
     return In;
}

or *preferrably*

istream &operator>>(istream &In,StrC &Copy)
{
     string Buffer;
     getline (In,Buffer);
     delete [] Copy.TextBuffer;
     Copy.TextBuffer = new char[Buffer.length()];
     strcpy(Copy.TextBuffer,Buffer.c_str());
     return In;
}

Why not use a 'std::string', since it makes like *so* much easier?
0
 
List244Author Commented:
Haha, JKR, you should know by now that I know I can use string...  So how does the string class work?
If it can accept strings like this, shouldn't I be able to?  Does the string class have a max size for use with
cin?
0
 
jkrCommented:
>>Haha, JKR, you should know by now that I know I can use string...  So how does the string class work?

That's why I was slightly surprised why you didn't use that in the 1st place ;o)

You can just use it as in my example above. You might also want to consider changing 'TextBuffer' in 'StrC' to a 'string' also, this will save you from all that error prone 'new', 'delete' and Ă„strcpy()' stuff.

>>Does the string class have a max size for use with cin?

Theoretically somewhere around 2GB *g*
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
List244Author Commented:
Well, the main part of this class, StrC, is recreating the string class.  However, minus most of the functions,
and with a few extra.  If I used the string class inside of it, I would basically just be parsing a string, and that
is too easy, I want to do it without strings, more fun. :P
0
 
jkrCommented:
OK, then replacing that by a string would probably defeat the purpose. BTW, I just noticed that you were omitting the 'streamsize' parameter, so that should be

     char* Buffer = new char[MAX_SIZE];
     In.getline (Buffer, MAX_SIZE, '\n');
0
 
List244Author Commented:
Yeah, I knew what you meant there.  Thanks again for the help.
0
 
itsmeandnobodyelseCommented:
Maybe you should change to that:

istream &operator>>(istream &In,StrC &Copy)
{
     char Buffer[1024];
     In.getline (Buffer, sizeof(Buffer), '\n');
     Copy = Buffer;
     return In;
}

>>>>     Copy = Buffer;

To make that work you need an appropriate assignment operator

StrC& StrC::operator=(const char* psz)
{
     delete [] TextBuffer;
     TextBuffer = new char[strlen(psz)+1];
     strcpy(TextBuffer, psz);
     return *this;
}

Regards, Alex
0
 
List244Author Commented:
Itsmeandnobodyelse:

I have:

istream &operator>>(istream &In,StrC &Copy)
{
      char Buffer[1001];
      In.getline(Buffer,1001,'\n');
      delete [] Copy.TextBuffer;
      Copy.TextBuffer = new char[strlen(Buffer)];
      strcpy(Copy.TextBuffer,Buffer);
      return In;
}

What benefits come from changing it to:

istream &operator>>(istream &In,StrC &Copy)
{
      char Buffer[1001];
      In.getline(Buffer,1001,'\n');
      Copy = Buffer;
      return In;
}

PS: I already have an overloaded = which works similar to yours.
0
 
itsmeandnobodyelseCommented:
>>>> What benefits come from changing it to

A simple benefit is, that it is shorter and much less error-prone. Furthermore, it is object-oriented programming (OOP). OOP means that the objects provide suitable member functions, e. g. assignment operators, that you can use them without having to know their internal implementation. That's why members like StrC::TextBuffer normally were defined private. And it isn't recommendable to modify data members directly from a non-member operator function. If you decide to change your string class. e. g. to support wide characters or reference counting, the operator>> functions don't need to be changed if they only use standard interfaces.

>>>> In.getline(Buffer,1001,'\n');

Use
 
    In.getline (Buffer, sizeof(Buffer), '\n');

instead. Then, you have to change only one parameter.


Regards, Alex
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now