toString() ?

A

An Ony

Hi,
Is there a default toString() function in c++ like in java?

Having a class Person with a firstname and lastname attribute.
Can I create something that will printout the name and use it like this?

Person p;
//make it have a name
cout << p;

Or is this too much magic asked and should I just do a p->printMe()
function?

Thx!
(yes, I'm a java programmer)
 
R

Rolf Magnus

An said:
Hi,
Is there a default toString() function in c++ like in java?
No.

Having a class Person with a firstname and lastname attribute.
Can I create something that will printout the name and use it like this?

Person p;
//make it have a name
cout << p;

Yes. Write an operator<<. Something like like:

std::eek:stream& operator<<(std::eek:stream& s, const Person& p)
{
s << p.firstname() << " " << p.lastname();
}
 
D

Derrick Coetzee

An said:
Is there a default toString() function in c++ like in java?

There's something very much like it, but more flexible and not as succinct:

#include <sstream>

std::eek:stringstream temp;
temp << yourObject;
std::string result = temp.str();

Not every object need have such an operator defined, but many do, at
least for debugging purposes.
 
M

Mike Wahler

An Ony said:
Hi,
Is there a default toString() function in c++ like in java?
No.

Having a class Person with a firstname and lastname attribute.
Can I create something that will printout the name and use it like this?

Person p;
//make it have a name
cout << p;
Yes.

Or is this too much magic asked

Not at all. Use operator overloading:

#include <iostream>
#include <ostream>
#include <string>

class Person
{
public:
Person(const std::string& fn, const std::string& ln)
: first(fn), last(ln)
{
}

private:
std::string first;
std::string last;

friend std::eek:stream& operator<<(std::eek:stream& os, const Person& p)
{
return os << p.first << ' ' << p.last;
}

};

int main()
{
Person p("John", "Doe");
std::cout << p << '\n';
return 0;
}

and should I just do a p->printMe()
function?

That's another possiblity (e.g. if you don't want to define
a friend function):

#include <iostream>
#include <ostream>
#include <string>

class Person
{
public:
Person(const std::string& fn, const std::string& ln)
: first(fn), last(ln)
{
}

std::eek:stream& out(std::eek:stream& os) const
{
return os << first << ' ' << last;
}

private:
std::string first;
std::string last;
};

std::eek:stream& operator<<(std::eek:stream& os, const Person& p)
{
return p.out(os);
}

int main()
{
Person p("John", "Doe");
std::cout << p << '\n';
return 0;
}

-Mike
 
R

rossum

Yes. Write an operator<<. Something like like:

std::eek:stream& operator<<(std::eek:stream& s, const Person& p)
{
s << p.firstname() << " " << p.lastname(); return s;
}

rossum
 

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

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top