object to standard output and input ( cout / cin )

G

gouki

suppose i have a class:


class X
{
int i;
string out;
}

X::eek:ut = "object x";


how can i do this:
code:
X obj;
cout<<obj;

output:
objext x
and

code:
X obj;
cin>>obj

is this possible???

thanks. =)
 
B

Buster

gouki said:
suppose i have a class:


class X
{
int i;
string out;
}

You forgot the semicolon. Also, the class is unusable because it has no
friends and no public members. Assume you have this instead:

// Header file "X.h"

#include <iosfwd>
#include <string>

class X
{
int i;
std::string out;

// These will come in handy later.
friend std::eek:stream & operator << (std::eek:stream &, const X &);
friend std::istream & operator >> (std::istream &, X &);

public:
X (int i, const std::string & out) : i (i), out (out) { }
};
X::eek:ut = "object x";

(Another syntax error. As a non-static member of X, out can only be
accessed for a particular X object, using the . or -> operator.)
how can i do this:
code:
X obj;
cout<<obj;

output:
objext x
and

code:
X obj;
cin>>obj

is this possible???

Yep. Overload the << and >> operators. Here's your first draft. This is
not well-tested code, and it artificially constrains X (if an X object
is to be successfully streamed out and then back in again, the "out"
member cannot contain a newline), but you get the idea.

// Source file X_io.cpp

#include "X.h"
#include <ostream>
#include <istream>
#include <sstream>

std::eek:stream & operator << (std::eek:stream & stream, const X & x)
{
return stream << x.i << '\n' << x.out << '\n';

// That was easy!
}

std::istream & operator >> (std::istream & stream, X & x)
{
// For extractors we usually have to work a bit harder.

int i;
std::string out;

// Read 'i' and 'out' as single lines from 'stream'.

{
std::string t;
std::getline (stream, t);
std::istringstream sstream (t);
sstream >> i;
}
std::getline (stream, out); // Read "out".

// Commit results if no stream error.

if (stream)
{
x.i = i;
x.out.swap (out); // We can't risk a bad_alloc at this stage.
}
return stream;
}

// Source file "test.cpp"

#include "X.h"
#include <string>
#include <sstream>
#include <iostream>
#include <cstddef>

X x_from_string (const std::string & s)
{
X result (0, "");
std::istringstream sstream (s);
sstream >> result;
return result;
}

std::string x_to_string (const X & x)
{
std::eek:stringstream sstream;
sstream << x;
return sstream.str ();
}

int main (int argc, char * argv [])
{
std::string persistent;

{
X old (163, "262 537 412 640 768 744");
persistent = x_to_string (old);
}

X lazarus (x_from_string (persistent));

std::cout << lazarus;
return EXIT_SUCCESS;
}
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top