Sign in to follow this  
johnnyBravo

With get strings, should I do std::string getName, or getName(std::string* name)??

Recommended Posts

Hi, I was wondering when using the std::string class with a get method, should I do this: std::string getName(); or this: void getName(std::string* name); Like I know its said usually when you are using char* for a string you should do the second one, but i'm not sure with the string class. Thanks.

Share this post


Link to post
Share on other sites
I tend to do const std::string &getname()

this way the function can be used for function parameters.

I really dont like

std::string tempstring;
object->getname(tempstring);
otherobject->somefunc(tempstring);



when you could do the same in 1 line

somefunc(object->getname());




I avoid std::string getname() as that will return a copy of the string, basically causing memory allocation every time you call it, so I favor returning a const reference.

Additionally, if you do


const char *getname()
{
return str.c_str();
}




And you call a function with this

somefunc(object->getname());




but the function takes an std::string I'm pretty sure that it will cause a temporary string to be created(someone correct me if I'm wrong), because somefunc wants a string but the getname() returns a char *.

EDIT: Christ, do the source boxes have to be so big? Can they make them auto-size?

Share this post


Link to post
Share on other sites
i see drevil, so with the setName, i should use setname(const char name[]) aswell?
instead of a non const
Quote:
Original post by Anonymous Poster

const std::string &getName()


and that & means its a reference? and why not use a pointer instead?


thanks

Share this post


Link to post
Share on other sites
no I would use setname(const std::string &_str)

IMO if you're going to use std::strings use them everywhere, then you can pass references and stuff around easily and quickly. If you mix std::string with some functions that use char *, I think there is going to be some temporary strings being created.

Quote:

and that & means its a reference? and why not use a pointer instead?


Yea, a reference is similar to a pointer, but you dont have to worry about dereferencing a reference like you do a pointer. It's cleaner.


std::string mystring;
void setname(const std::string &_str)
{
mystring = _str;
}

void setname(const std::string *_str)
{
mystring = *_str;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by johnnyBravo
It isn't possible to change the original name's value some how is it?
It is, and this is one of the uses of returning a reference:

App foo;
foo.getName() = "new name";

If you don't want this, then return a const reference.
Quote:
Original post by joanusdmentia
NEVER do this!!! The value returned by c_str() is not guarentteed to always be valid, so returning it from a function is a REALLY BAD IDEA!!!
To clarify, the value returned by c_str() is guaranteed to be valid until the string is modified.

Share this post


Link to post
Share on other sites
Quote:
Original post by joanusdmentia
Quote:
Original post by Dr Evil
const char *getname()
{
return str.c_str();
}

NEVER do this!!! The value returned by c_str() is not guarentteed to always be valid, so returning it from a function is a REALLY BAD IDEA!!!
not quite correct about valid-ness, and beer hunter is right, valid till changed.

that function is one of the worst possible ways to return a string.
1) what happens when the st::string object changes,
2) what happens if the class object that owns the std::string as a member is deleted or goes out of scope?
3) thats just really bad bad code.

const std::string getName() const { 
return name;
}
realise that this function is in most cases for code clarity and will almost never affect performace (given it is as simple as the above example). chances are the compiler will optimize away the construction of the std::string return value and i'd bet that it would even optimize away the function call all together.

dont be affraid to use vivid code, the clearer your intentions are to the compiler the better job it can do at optimizing.

Share this post


Link to post
Share on other sites
Quote:
Original post by silvermace
Quote:
Original post by joanusdmentia
Quote:
Original post by Dr Evil
const char *getname()
{
return str.c_str();
}

NEVER do this!!! The value returned by c_str() is not guarentteed to always be valid, so returning it from a function is a REALLY BAD IDEA!!!
not quite correct about valid-ness, and beer hunter is right, valid till changed.

Yeah, I was 99% sure that's what was guarentteed by the standard, but saying 'not always valid' was that little bit safer [smile]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this