Connecting Tech Pros Worldwide Forums | Help | Site Map

overloading >> operator

Ashwin
Guest
 
Posts: n/a
#1: Jul 27 '06
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.


lallous
Guest
 
Posts: n/a
#2: Jul 27 '06

re: overloading >> operator


Hello

"Ashwin" <ashwin.bevinje@gmail.comwrote in message
news:1153979379.875817.62520@b28g2000cwb.googlegro ups.com...
Quote:
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
Quote:
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


Ashwin
Guest
 
Posts: n/a
#3: Jul 27 '06

re: overloading >> operator


lallous wrote:
Quote:
Hello
>
"Ashwin" <ashwin.bevinje@gmail.comwrote in message
news:1153979379.875817.62520@b28g2000cwb.googlegro ups.com...
Quote:
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
>
Quote:
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.

Ian Collins
Guest
 
Posts: n/a
#4: Jul 27 '06

re: overloading >> operator


Ashwin wrote:
Quote:
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.
WittyGuy
Guest
 
Posts: n/a
#5: Jul 27 '06

re: overloading >> operator


"Ashwin" <ashwin.bevinje@gmail.comwrote in message
news:1153979379.875817.62520@b28g2000cwb.googlegro ups.com...
Quote:
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.
Quote:
return in;
}
HTH,
Sukumar R


Ashwin
Guest
 
Posts: n/a
#6: Jul 27 '06

re: overloading >> operator


WittyGuy wrote:
Quote:
"Ashwin" <ashwin.bevinje@gmail.comwrote in message
news:1153979379.875817.62520@b28g2000cwb.googlegro ups.com...
Quote:
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.
>
Quote:
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.
Quote:
Quote:
}
>
HTH,
Sukumar R
Ian Collins
Guest
 
Posts: n/a
#7: Jul 27 '06

re: overloading >> operator


Ashwin wrote:
Quote:
>
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.
Ashwin
Guest
 
Posts: n/a
#8: Jul 27 '06

re: overloading >> operator



Ian Collins wrote:
Quote:
Ashwin wrote:
Quote:

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.

Rolf Magnus
Guest
 
Posts: n/a
#9: Jul 27 '06

re: overloading >> operator


Ashwin wrote:
Quote:
>
Ian Collins wrote:
>
Quote:
>Ashwin wrote:
Quote:
>
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);
Quote:
void operator=(string1 s);
string1& operator=(const string1& s);
Quote:
and
string1& operator+(char *s);
string1& operator+(const char *s);
Quote:
char operator[](unsigned int i);
char operator[](unsigned int i) const;
Quote:
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);
Quote:
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.

Ashwin
Guest
 
Posts: n/a
#10: Jul 27 '06

re: overloading >> operator



Rolf Magnus wrote:
Quote:
Ashwin wrote:
>
Quote:

Ian Collins wrote:
Quote:
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);
>
Quote:
void operator=(string1 s);
>
string1& operator=(const string1& s);
>
Quote:
and
string1& operator+(char *s);
>
string1& operator+(const char *s);
>
Quote:
char operator[](unsigned int i);
>
char operator[](unsigned int i) const;
>
Quote:
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);
>
Quote:
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'.

Ian Collins
Guest
 
Posts: n/a
#11: Jul 27 '06

re: overloading >> operator


Ashwin wrote:
Quote:
Quote:
Quote:
>>>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.
Rolf Magnus
Guest
 
Posts: n/a
#12: Jul 27 '06

re: overloading >> operator


Ashwin wrote:
Quote:
Quote:
Quote:
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);
>>
Quote:
void operator=(string1 s);
>>
>string1& operator=(const string1& s);
>>
Quote:
and
string1& operator+(char *s);
>>
>string1& operator+(const char *s);
>>
Quote:
char operator[](unsigned int i);
>>
>char operator[](unsigned int i) const;
>>
Quote:
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);
>>
Quote:
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.

Closed Thread