Click to See Complete Forum and Search --> : Functions
Is it possible to pass a string by reference?
For example, void SomeFunction(string&, string&);
I know this isn't correct but how can I pass strings by reference if at all possible?
dighn
04-04-2002, 10:23 PM
yes of course you can pass strings by reference. eg
using std::string;
void foo(string& bar)
{
bar = "barfoo";
}
void foobar()
{
string fubar = "foobar";
foo(fubar);
//now fubar is "barfoo";
}
Oliwa
04-05-2002, 09:59 AM
Why does everyone use Foo, Bar, and Foobar in examples?
That's funny Oliwa. I have noticed that to when I read some of the other posts.
krack_it_up
04-05-2002, 11:54 AM
hehe... usually if you have to ask how to do something... it is FUBR.
biosx
04-05-2002, 12:10 PM
That's FUBAR, not FUBR... I don't like how people use foobar b/c when you spell it differently it loses all it's meaning.
I learned about FUBAR in my cyperpunk fakebook I got from my stepmom when I was like 12.. Who knows why she bought me that book.
dighn
04-05-2002, 04:22 PM
it's like an unspoken rule to use foobar in your code examples although i think i over did it LOL :D
btw it's not "fubar" :)
!shira!
04-05-2002, 04:32 PM
Originally posted by dighn
yes of course you can pass strings by reference. eg
using std::string;
void foo(string& bar)
{
bar = "barfoo";
}
void foobar()
{
string fubar = "foobar";
foo(fubar);
//now fubar is "barfoo";
}
wouldn't it be
void foo(string* bar)
{
bar = "barfoo";
}
void foobar()
{
string fubar = "foobar";
foo(&fubar);
}
dighn
04-05-2002, 04:44 PM
Originally posted by !shira!
wouldn't it be
void foo(string* bar)
{
bar = "barfoo";
}
void foobar()
{
string fubar = "foobar";
foo(&fubar);
}
sorry but no :)
first of all that's using pointers
secondly bar = "barfoo" is illegal. it should be *bar = "barfoo";
with that little change it accomplishes the same thing but that's using pointers instead of references (pointer is type*, reference is type&) which was what he asked for.
!shira!
04-05-2002, 05:55 PM
I get it about refrences now and you were right about the illegal pointer call
The C programming language SE is a handy book (much easier reade than the first)