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

G

Goran

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
 
G

gnuyuva

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::eek:perator<<(const std::string& string)
{ ... }
 
J

Jim Langston

Victor said:
gnuyuva said:
[..]
MyClass_t& MyClass_t::eek:perator<<(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::eek:perator<<(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;

?
 
J

Jerry Coffin

(e-mail address removed)>, (e-mail address removed)
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;
 
R

Rolf Magnus

Jim said:
Victor said:
gnuyuva said:
[..]
MyClass_t& MyClass_t::eek:perator<<(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::eek:perator<<(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;
 
J

Jerry Coffin

Jerry said:
[..] 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.
 
J

Jim Langston

Rolf said:
Jim said:
Victor said:
gnuyuva wrote:
[..]
MyClass_t& MyClass_t::eek:perator<<(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::eek:perator<<(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.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top