|
-
Catfish
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?
-
Mako Shark
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";
}
-
Catfish
Why does everyone use Foo, Bar, and Foobar in examples?
-
Catfish
I know!!!
That's funny Oliwa. I have noticed that to when I read some of the other posts.
-
Hammerhead Shark
hehe... usually if you have to ask how to do something... it is FUBR.
MMMMMMMMMM...... BAR-B-Q
-- Homer Simpson
-
Reef Shark
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.
## root is the greed of all evil ##
-
Mako Shark
it's like an unspoken rule to use foobar in your code examples although i think i over did it LOL 
btw it's not "fubar"
-
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);
}
-
Mako Shark
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.
-
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|