473,466 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Newbie: How to store data into my object with << ?

Hi all,

I want to store data with << like this way:

myClass_t aObject;
string aString("foo");

// no output, just edit and store the string
aObject << aString;

// here comes the edited string
cout << aObject;

My question is:
How do I realize "aObject << aString;"? I've read a lot about
overloading operators and it's no prob. But what's the signature of
the operator which realizes "aObject << aString;"?

Thanks a lot

Goran
Jun 27 '08 #1
8 1100
On May 15, 5:56 pm, Goran <postmasch...@gmail.comwrote:
Hi all,

I want to store data with << like this way:

myClass_t aObject;
string aString("foo");

// no output, just edit and store the string
aObject << aString;

// here comes the edited string
cout << aObject;

My question is:
How do I realize "aObject << aString;"? I've read a lot about
overloading operators and it's no prob. But what's the signature of
the operator which realizes "aObject << aString;"?
MyClass_t& MyClass_t::operator<<(const std::string& string)
{ ... }

Jun 27 '08 #2
gnuyuva wrote:
[..]
MyClass_t& MyClass_t::operator<<(const std::string& string)
{ ... }
{ ...
return *this; // idiomatic
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #3
Victor Bazarov wrote:
gnuyuva wrote:
>[..]
MyClass_t& MyClass_t::operator<<(const std::string& string)
{ ... }

{ ...
return *this; // idiomatic
}

V
A question on this. I checked the own signature in my own code and found
this:

void CItem::operator<<(const std::string& sIn)
{
// ...
}

which, as far as my usage of it, is working fine. Although it's possible
that this is some extention, I've only compiled it under one compiler.

Is my signature wrong, another way to do it, poor design, okay, or?

My usage being like this:

std::istream& operator>>( std::istream& is, CItem& Item )
{
std::string Line;
if ( getline( is, Line ) )
Item << Line;
return is;
}

What purpose would/could be served by returning a referece to the instance?
Maybe returning it from some function?

return Item << Line;

?

--
Jim Langston
ta*******@rocketmail.com
Jun 27 '08 #4
In article <2ac1ff02-7630-4bf9-b60d-
43**********@l42g2000hsc.googlegroups.com>, po**********@gmail.com
says...
Hi all,

I want to store data with << like this way:

myClass_t aObject;
string aString("foo");

// no output, just edit and store the string
aObject << aString;

// here comes the edited string
cout << aObject;

My question is:
How do I realize "aObject << aString;"? I've read a lot about
overloading operators and it's no prob. But what's the signature of
the operator which realizes "aObject << aString;"?
You've already gotten one answer, but I'll add another that overloads
operator<< with a non-member function:

myClass_t &operator<<(myClass_t &sink, aString const &source);

As for why you return an instance of the stream-like object, it's to
allow chaining the operator, so you can do things like:

myCLass_t aObject;
string aString("foo"), bString("bar");

aObject << aString << bString;

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #5
Jim Langston wrote:
Victor Bazarov wrote:
>gnuyuva wrote:
>>[..]
MyClass_t& MyClass_t::operator<<(const std::string& string)
{ ... }

{ ...
return *this; // idiomatic
}

V

A question on this. I checked the own signature in my own code and found
this:

void CItem::operator<<(const std::string& sIn)
{
// ...
}

which, as far as my usage of it, is working fine.
Why shouldn'it?
Although it's possible that this is some extention,
What?
I've only compiled it under one compiler.
Is my signature wrong, another way to do it, poor design, okay, or?
Oh, you mean because you returned void? No, that's fine.
My usage being like this:

std::istream& operator>>( std::istream& is, CItem& Item )
{
std::string Line;
if ( getline( is, Line ) )
Item << Line;
return is;
}

What purpose would/could be served by returning a referece to the
instance? Maybe returning it from some function?

return Item << Line;

?
It makes it possible to concatenate several operator calls:

Item << Line << Line2;

Jun 27 '08 #6
Jerry Coffin wrote:
[..] I'll add another that overloads
operator<< with a non-member function:

myClass_t &operator<<(myClass_t &sink, aString const &source);
Usually operators that change the object should probably be members of
the class (IMNSHO); the whole point of making some operators non-members
is to allow conversions applied to the left-hand-side operand (which is
not the case here, I guess).
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #7
In article <g0**********@news.datemas.de>, v.********@comAcast.net
says...
Jerry Coffin wrote:
[..] I'll add another that overloads
operator<< with a non-member function:

myClass_t &operator<<(myClass_t &sink, aString const &source);

Usually operators that change the object should probably be members of
the class (IMNSHO); the whole point of making some operators non-members
is to allow conversions applied to the left-hand-side operand (which is
not the case here, I guess).
One point is to allow conversions -- another is to allow extension
without modification.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #8
Rolf Magnus wrote:
Jim Langston wrote:
>Victor Bazarov wrote:
>>gnuyuva wrote:
[..]
MyClass_t& MyClass_t::operator<<(const std::string& string)
{ ... }

{ ...
return *this; // idiomatic
}

V

A question on this. I checked the own signature in my own code and
found this:

void CItem::operator<<(const std::string& sIn)
{
// ...
}

which, as far as my usage of it, is working fine.

Why shouldn'it?
>Although it's possible that this is some extention,

What?
>I've only compiled it under one compiler.
>Is my signature wrong, another way to do it, poor design, okay, or?

Oh, you mean because you returned void? No, that's fine.
>My usage being like this:

std::istream& operator>>( std::istream& is, CItem& Item )
{
std::string Line;
if ( getline( is, Line ) )
Item << Line;
return is;
}

What purpose would/could be served by returning a referece to the
instance? Maybe returning it from some function?

return Item << Line;

?

It makes it possible to concatenate several operator calls:

Item << Line << Line2;
Ahh, yes. That clears it up then. In my case that line is (and should be)
an error. So void is perfectly fine for me. Thanks.

--
Jim Langston
ta*******@rocketmail.com
Jun 27 '08 #9

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

Similar topics

11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
2
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form of list< vector<string> > is correct in STL ? //...
1
by: masoumeh | last post by:
please help me I use this code TA_Insert_Language as new DataSetName.Insert_LanguageTableAdapter DT_Insert_Language as new DataSetName.Insert_LanguageDataTable dim MaxRow as new DataRow dim...
4
by: Torsten Z | last post by:
Hi, how can I create an object to get the following XML by serialization <ServiceHost> <HostId>cdf87f74-d15c-44a1-9c77-c03c2c5c1588</HostId> <ScreenIds>
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
4
by: Samuel | last post by:
Hi, Say you have the following XML: <item ref="1"> <name>item 1</name> </item> <item ref="2"> <name>item 2</name> </item>
0
by: Orgil | last post by:
Hi all, I have just writing a program named "DayBook". I am using MS Access 2003 database and Microsoft .NET C# 2005 (with framework 2.0). MS Access 2003, MS .NET C# 2005 and dotNetFramework2.0...
1
by: inteladu | last post by:
I have created an object TF_T.....and I seem to be stuck at this error....please need some suggestions to overcome this.....please....very very urgent!!! thanks!!!
5
bilibytes
by: bilibytes | last post by:
Hi, I am wondering how to print '<?php' when y try or to store '<?php' string or to print it, there is no output i think this is or a php.ini directive or a httpd.conf one does someone...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.