Functions

Sharky Forums


Results 1 to 10 of 10

Thread: Functions

  1. #1
    Catfish f15e's Avatar
    Join Date
    Mar 2002
    Location
    I live in the south.
    Posts
    114

    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?

  2. #2
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171
    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";
    }
    .

  3. #3
    Catfish Oliwa's Avatar
    Join Date
    Jun 2001
    Location
    A hole in the wall
    Posts
    101
    Why does everyone use Foo, Bar, and Foobar in examples?

  4. #4
    Catfish f15e's Avatar
    Join Date
    Mar 2002
    Location
    I live in the south.
    Posts
    114

    I know!!!

    That's funny Oliwa. I have noticed that to when I read some of the other posts.

  5. #5
    Hammerhead Shark krack_it_up's Avatar
    Join Date
    Feb 2001
    Location
    Phoenix, AZ USA... the one just south of Canada
    Posts
    1,831
    hehe... usually if you have to ask how to do something... it is FUBR.
    MMMMMMMMMM...... BAR-B-Q
    -- Homer Simpson

  6. #6
    Reef Shark biosx's Avatar
    Join Date
    Jun 2001
    Location
    Chicago, IL, USA
    Posts
    448
    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 ##

  7. #7
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171
    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"
    .

  8. #8
    Hammerhead Shark
    Join Date
    Jan 2002
    Location
    0xC12F6D9A
    Posts
    1,600
    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);
    }

  9. #9
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171
    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.
    .

  10. #10
    Hammerhead Shark
    Join Date
    Jan 2002
    Location
    0xC12F6D9A
    Posts
    1,600
    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
  •