overloading >> operator

Discussion in 'C++' started by Ashwin, Jul 27, 2006.

  1. Ashwin

    Ashwin Guest

    hi guys,

    i have overloaded >> operator for my own string class .the function is
    as given below
    istream& operator>>(istream &in,string1 &s)
    {
    char *a;
    a=new char[100];
    in>>a;
    s=a;
    return in;
    }
    i have also tried using a[100] and then using in>>a;
    but in both the cases if i enter a string it gives segmentation fault.
    and if i enter a character it works properly. if i give more than one
    character it gives segmentation fault.what is the reason for this plzz
    explain.
    thanks in advance.
     
    Ashwin, Jul 27, 2006
    #1
    1. Advertising

  2. Ashwin

    lallous Guest

    Hello

    "Ashwin" <> wrote in message
    news:...
    > hi guys,
    >
    > i have overloaded >> operator for my own string class .the function is
    > as given below
    > istream& operator>>(istream &in,string1 &s)
    > {
    > char *a;
    > a=new char[100];
    > in>>a;
    > s=a;
    > return in;
    > }
    >


    Very bad code.
    1. memory leaks
    2. can buffer overflow

    > i have also tried using a[100] and then using in>>a;
    > but in both the cases if i enter a string it gives segmentation fault.
    > and if i enter a character it works properly. if i give more than one
    > character it gives segmentation fault.what is the reason for this plzz
    > explain.
    >


    Are you entering a string bigger than 100 characters?

    Can't you trace your code and see where exactly the crash occurs?

    --
    Elias
     
    lallous, Jul 27, 2006
    #2
    1. Advertising

  3. Ashwin

    Ashwin Guest

    lallous wrote:

    > Hello
    >
    > "Ashwin" <> wrote in message
    > news:...
    > > hi guys,
    > >
    > > i have overloaded >> operator for my own string class .the function is
    > > as given below
    > > istream& operator>>(istream &in,string1 &s)
    > > {
    > > char *a;
    > > a=new char[100];
    > > in>>a;
    > > s=a;
    > > return in;
    > > }
    > >

    >
    > Very bad code.
    > 1. memory leaks
    > 2. can buffer overflow
    >
    > > i have also tried using a[100] and then using in>>a;
    > > but in both the cases if i enter a string it gives segmentation fault.
    > > and if i enter a character it works properly. if i give more than one
    > > character it gives segmentation fault.what is the reason for this plzz
    > > explain.
    > >

    >
    > Are you entering a string bigger than 100 characters?
    >
    > Can't you trace your code and see where exactly the crash occurs?
    >
    > --
    > Elias


    thanks elias

    can u explain me about memory leaks or give me any good links about
    memory leak.i think there is a buffer overflow i am checking for it.
     
    Ashwin, Jul 27, 2006
    #3
  4. Ashwin

    Ian Collins Guest

    Ashwin wrote:
    > hi guys,
    >
    > i have overloaded >> operator for my own string class .the function is
    > as given below
    > istream& operator>>(istream &in,string1 &s)
    > {
    > char *a;
    > a=new char[100];
    > in>>a;


    Don't do this, use getline();

    --
    Ian Collins.
     
    Ian Collins, Jul 27, 2006
    #4
  5. Ashwin

    WittyGuy Guest

    "Ashwin" <> wrote in message
    news:...
    > hi guys,
    >
    > i have overloaded >> operator for my own string class .the function is
    > as given below
    > istream& operator>>(istream &in,string1 &s)
    > {
    > char *a;
    > a=new char[100];
    > in>>a;
    > s=a;


    Ashwin,
    Try doing deep copy instead of shallow copy. And don't forget to delete the
    allocated memory.

    > return in;
    > }


    HTH,
    Sukumar R
     
    WittyGuy, Jul 27, 2006
    #5
  6. Ashwin

    Ashwin Guest

    WittyGuy wrote:

    > "Ashwin" <> wrote in message
    > news:...
    > > hi guys,
    > >
    > > i have overloaded >> operator for my own string class .the function is
    > > as given below
    > > istream& operator>>(istream &in,string1 &s)
    > > {
    > > char *a;
    > > a=new char[100];
    > > in>>a;
    > > s=a;

    >
    > Ashwin,
    > Try doing deep copy instead of shallow copy. And don't forget to delete the
    > allocated memory.
    >
    > > return in;


    thanks for the reponse can anyone tell me how to implement the
    following eqation
    s[1]='a';
    where s is a object of the string class i have written
    plzz tell me how to overload the operations just signatures of the
    functions.
    > > }

    >
    > HTH,
    > Sukumar R
     
    Ashwin, Jul 27, 2006
    #6
  7. Ashwin

    Ian Collins Guest

    Ashwin wrote:
    >
    > thanks for the reponse can anyone tell me how to implement the
    > following eqation
    > s[1]='a';
    > where s is a object of the string class i have written
    > plzz tell me how to overload the operations just signatures of the
    > functions.
    >

    Show us what you have so far.

    Please stop using silly abbreviations like 'plz' and try to use correct
    capitalisation. Otherwise your posts are hard to read an people who
    might help you will ignore them.


    --
    Ian Collins.
     
    Ian Collins, Jul 27, 2006
    #7
  8. Ashwin

    Ashwin Guest

    Ian Collins wrote:

    > Ashwin wrote:
    > >
    > > thanks for the reponse can anyone tell me how to implement the
    > > following eqation
    > > s[1]='a';
    > > where s is a object of the string class i have written
    > > plzz tell me how to overload the operations just signatures of the
    > > functions.
    > >

    > Show us what you have so far.
    >
    > Please stop using silly abbreviations like 'plz' and try to use correct
    > capitalisation. Otherwise your posts are hard to read an people who
    > might help you will ignore them.
    >
    >
    > --
    > Ian Collins.

    sorry Ian collins i wont use that abbrevation again . i have overloaded
    both [ ] and = operator as follows
    void operator=(char *s);
    void operator=(string1 s);
    and
    string1& operator+(char *s);
    char operator[](unsigned int i);
    where string1 is the string class i am writing.
    so to implement the expression s[1]='c';
    i donot know how to overload the operators
    i am thinking of using char operator=(char ch);
    and char operator[ ] (unsigned int i);
    i don't know whether it will work or not have to try.
     
    Ashwin, Jul 27, 2006
    #8
  9. Ashwin

    Rolf Magnus Guest

    Ashwin wrote:

    >
    > Ian Collins wrote:
    >
    >> Ashwin wrote:
    >> >
    >> > thanks for the reponse can anyone tell me how to implement the
    >> > following eqation
    >> > s[1]='a';
    >> > where s is a object of the string class i have written
    >> > plzz tell me how to overload the operations just signatures of the
    >> > functions.
    >> >

    >> Show us what you have so far.
    >>
    >> Please stop using silly abbreviations like 'plz' and try to use correct
    >> capitalisation. Otherwise your posts are hard to read an people who
    >> might help you will ignore them.
    >>
    >>
    >> --
    >> Ian Collins.

    > sorry Ian collins i wont use that abbrevation again . i have overloaded
    > both [ ] and = operator as follows
    > void operator=(char *s);


    Should better be:

    string1& operator=(const char* s);

    > void operator=(string1 s);


    string1& operator=(const string1& s);

    > and
    > string1& operator+(char *s);


    string1& operator+(const char *s);

    > char operator[](unsigned int i);


    char operator[](unsigned int i) const;

    > where string1 is the string class i am writing.
    > so to implement the expression s[1]='c';
    > i donot know how to overload the operators


    Try to add:

    char& operator[](unsigned int i);

    > i am thinking of using char operator=(char ch);
    > and char operator[ ] (unsigned int i);
    > i don't know whether it will work or not have to try.


    Doesn't make much sense.
     
    Rolf Magnus, Jul 27, 2006
    #9
  10. Ashwin

    Ashwin Guest

    Rolf Magnus wrote:
    > Ashwin wrote:
    >
    > >
    > > Ian Collins wrote:
    > >
    > >> Ashwin wrote:
    > >> >
    > >> > thanks for the reponse can anyone tell me how to implement the
    > >> > following eqation
    > >> > s[1]='a';
    > >> > where s is a object of the string class i have written
    > >> > plzz tell me how to overload the operations just signatures of the
    > >> > functions.
    > >> >
    > >> Show us what you have so far.
    > >>
    > >> Please stop using silly abbreviations like 'plz' and try to use correct
    > >> capitalisation. Otherwise your posts are hard to read an people who
    > >> might help you will ignore them.
    > >>
    > >>
    > >> --
    > >> Ian Collins.

    > > sorry Ian collins i wont use that abbrevation again . i have overloaded
    > > both [ ] and = operator as follows
    > > void operator=(char *s);

    >
    > Should better be:
    >
    > string1& operator=(const char* s);
    >
    > > void operator=(string1 s);

    >
    > string1& operator=(const string1& s);
    >
    > > and
    > > string1& operator+(char *s);

    >
    > string1& operator+(const char *s);
    >
    > > char operator[](unsigned int i);

    >
    > char operator[](unsigned int i) const;
    >
    > > where string1 is the string class i am writing.
    > > so to implement the expression s[1]='c';
    > > i donot know how to overload the operators

    >
    > Try to add:
    >
    > char& operator[](unsigned int i);
    >
    > > i am thinking of using char operator=(char ch);
    > > and char operator[ ] (unsigned int i);
    > > i don't know whether it will work or not have to try.

    >
    > Doesn't make much sense.

    hi Rolf,

    i wanted to know how to overload the [] and = operator so that i can
    implement the expression
    s[1]='c'.
     
    Ashwin, Jul 27, 2006
    #10
  11. Ashwin

    Ian Collins Guest

    Ashwin wrote:
    >>>i am thinking of using char operator=(char ch);
    >>>and char operator[ ] (unsigned int i);
    >>>i don't know whether it will work or not have to try.

    >>
    >>Doesn't make much sense.

    >
    > hi Rolf,
    >
    > i wanted to know how to overload the [] and = operator so that i can
    > implement the expression
    > s[1]='c'.
    >

    This isn't trivial, you have to differentiate between lvalue and rvalue
    usage of operator[]. The normal technique is to return a proxy object
    which supports assignment from char and conversion to char.

    The best explanation I have seen of this in in Scott Meyers book "More
    Effective C++"

    --
    Ian Collins.
     
    Ian Collins, Jul 27, 2006
    #11
  12. Ashwin

    Rolf Magnus Guest

    Ashwin wrote:

    >> > sorry Ian collins i wont use that abbrevation again . i have overloaded
    >> > both [ ] and = operator as follows
    >> > void operator=(char *s);

    >>
    >> Should better be:
    >>
    >> string1& operator=(const char* s);
    >>
    >> > void operator=(string1 s);

    >>
    >> string1& operator=(const string1& s);
    >>
    >> > and
    >> > string1& operator+(char *s);

    >>
    >> string1& operator+(const char *s);
    >>
    >> > char operator[](unsigned int i);

    >>
    >> char operator[](unsigned int i) const;
    >>
    >> > where string1 is the string class i am writing.
    >> > so to implement the expression s[1]='c';
    >> > i donot know how to overload the operators

    >>
    >> Try to add:
    >>
    >> char& operator[](unsigned int i);
    >>
    >> > i am thinking of using char operator=(char ch);
    >> > and char operator[ ] (unsigned int i);
    >> > i don't know whether it will work or not have to try.

    >>
    >> Doesn't make much sense.

    > hi Rolf,
    >
    > i wanted to know how to overload the [] and = operator so that i can
    > implement the expression
    > s[1]='c'.


    And I told you that you can't do it that way, but need to define a

    char& operator[](unsigned int i);

    instead.
     
    Rolf Magnus, Jul 27, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith
    Replies:
    2
    Views:
    447
    Ivan Vecerina
    Oct 6, 2004
  2. Replies:
    11
    Views:
    765
    James Kanze
    May 16, 2007
  3. hurcan solter
    Replies:
    3
    Views:
    754
    Cholo Lennon
    Aug 29, 2007
  4. Replies:
    11
    Views:
    582
  5. Replies:
    2
    Views:
    334
Loading...

Share This Page