overloading >> operator

A

Ashwin

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.
 
L

lallous

Hello

Ashwin said:
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?
 
A

Ashwin

lallous said:
Hello

Ashwin said:
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?

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.
 
I

Ian Collins

Ashwin said:
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();
 
W

WittyGuy

Ashwin said:
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
 
A

Ashwin

WittyGuy said:
Ashwin said:
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.
 
I

Ian Collins

Ashwin said:
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.
 
A

Ashwin

Ian said:
Ashwin said:
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.
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.
 
R

Rolf Magnus

Ashwin said:
Ian said:
Ashwin said:
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.
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.
 
A

Ashwin

Rolf said:
Ashwin said:
Ian said:
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.
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'.
 
I

Ian Collins

Ashwin said:
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++"
 
R

Rolf Magnus

Ashwin said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top