473,833 Members | 2,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overloading >> operator

hi guys,

i have overloaded >operator for my own string class .the function is
as given below
istream& operator>>(istr eam &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.

Jul 27 '06
11 2086
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.
Jul 27 '06 #11
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=(strin g1 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.

Jul 27 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1774
by: maths_fan | last post by:
Can't understand how to overload these operations and for what reason?
4
1882
by: Don Hedgpeth | last post by:
Here's a question - I'm new to c++ and I have two classes that overload the >> operator. One class calls the other...such as. //code for class1 friend std::istream& operator >> (std::istream& lhs, class1& rhs) { .....random code here return lhs:} //code for class2 private: class1 jimbo;
4
2629
by: jelle | last post by:
Hi, I use python quite a bit to couple different programs together. Doing so has been a _lot_ easier since subprocess came around, but would really like to be able to use the succinct shell syntax; >, <, | That really shouldn't be too hard to wrap in a class, but so far I didn't succeed to do so this well, since I'm facing some trouble with operator precedence that I do not know how to overcome.
3
385
by: md | last post by:
Hi, the following code is working for static objects. ie the statement IntArray x(20); my problem is i want to use this overloading operator for dynamically created objects for example the statement IntArray *x; Please give me some help regarding this ////////////////////////////////////////////////////////////////////////////////////////////////////////// class IntArray
2
3551
by: nayannovellus | last post by:
As we know that both copy constructors and overloading = opeartor copies one object to another then whats the difference between copy constructor and overloading = operator. There must be some difference ... right?
3
2070
by: Aff | last post by:
hi, i wold like to ask, why is it when overloading operator we send int value is there any way around?. e.g. class abc { protected: int *a; private: int operator(int );
2
2506
by: Wayne Marsh | last post by:
Hello, Is it considered sane/good practice to write a global operator for the insertion and extraction operators of an fstream in binary mode to serialize a binary class, or are they strictly meant for formatted text input and output? Let's imagine, for example, that I had a standard Windows BMP file (I am aware that C++ has no concept of a BMP - this is simply putting my question in a simple context). If I wanted to load it into a...
11
4148
by: dascandy | last post by:
Hello, I was wondering, why is overloading operator. (period) forbidden? It would make a few odd applications possible (dynamic inheritance and transparent remote method invocation spring to my mind) and it would be fairly generic. The only sidecase I can see is that operator. itself would not be looked up through operator. . I read that there was previous debate on the subject, but I haven't been able to find why it was rejected.
0
2035
by: citystud | last post by:
I am trying to convert the c++ code to C#, but find difficulty to convert the overloading operator. Here is the source code. I don't really know how to implement the operator = and operator () in C#. #ifndef _OFFMAT_HPP_ #define _OFFMAT_HPP_ #include "fdct_wrapping_inc.hpp" using std::ostream; using std::istream;
0
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10782
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10500
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10543
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7753
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5789
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4422
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3972
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3078
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.