quick questions

Discussion in 'C++' started by Steve Chow, Sep 11, 2006.

  1. Steve Chow

    Steve Chow Guest

    i haven't seen these in any tutorials or anything i've read so i'm
    wondering if someone could tell me what they're called so i can
    research them.

    i've only seen functions like

    animal.make_noise("fart");

    but the other day i ran into something like

    animal.sounds().make_noise("fart");

    is there a name for calling multiple functions like this?
     
    Steve Chow, Sep 11, 2006
    #1
    1. Advertising

  2. Steve Chow

    Jens Theisen Guest

    "Steve Chow" <> writes:

    > animal.sounds().make_noise("fart");
    >
    > is there a name for calling multiple functions like this?


    No, but there is also nothing special about it.

    animial has a member function sounds, which returns some object x
    which has in turn a member function make_noise taking a string.

    The above line could happily be java with the same semantics.

    Cheers,

    Jens
     
    Jens Theisen, Sep 11, 2006
    #2
    1. Advertising

  3. Steve Chow wrote:

    > i haven't seen these in any tutorials or anything i've read so i'm
    > wondering if someone could tell me what they're called so i can
    > research them.
    >
    > i've only seen functions like
    >
    > animal.make_noise("fart");
    >
    > but the other day i ran into something like
    >
    > animal.sounds().make_noise("fart");
    >
    > is there a name for calling multiple functions like this?
    >


    It's not that two methods sounds () and make_noise (...) are called for
    the animal object, but rather only sounds (). This method would return
    another object that we don't know (at least I guess this, else the code
    wouldn't compile or make sense). For this object the method
    make_noise(...) is called.

    As far as I know there is no special name for such a mechanism. If
    sounds () were a method that returned the animal object, this would be
    called 'call chaining'. This is used for reading and writing formatted
    data with IO streams. Consider for example operator<< for streams. Using
    this you can write statements like
    cout << "Some text" << iSomeNumber << "AnotherText";
    This statement could be written as
    cout << "Some text";
    cout << iSomeNumber;
    cout << "AnotherText";
    Since it would be tedious to repeat 'cout << ' over and over again, the
    operator<< for streams should output the argument and return the stream.

    Regards,
    Stuart
     
    Stuart Redmann, Sep 11, 2006
    #3

  4. > animal.sounds().make_noise("fart");
    > is there a name for calling multiple functions like this?


    explicit lyrics?
     
    Gernot Frisch, Sep 11, 2006
    #4
  5. Steve Chow posted:

    > animal.sounds().make_noise("fart");



    This is equivalent to:

    ( animal.sounds() ).make_noise("fart");


    As you can see, the "sounds" member function is invoked first, yielding an
    expression. The "make_noise" member function is then invoked upon this
    expression. An example would be:

    class SoundSystem {
    public:

    void make_noise(char const *) const {}
    };

    class Animal {
    private:

    SoundSystem sndsys;

    public:

    SoundSystem &sounds()
    {
    return sndsys;
    }
    };

    int main()
    {
    Animal animal;

    animal.sounds().make_noise("fart");
    }

    --

    Frederick Gotham
     
    Frederick Gotham, Sep 11, 2006
    #5
  6. tragomaskhalos, Sep 11, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    3
    Views:
    380
  2. JS

    Two quick questions

    JS, Jan 18, 2006, in forum: Java
    Replies:
    7
    Views:
    362
    Roedy Green
    Jan 18, 2006
  3. Geoff Hague
    Replies:
    5
    Views:
    449
    Mark Parnell
    Jul 15, 2004
  4. Horny Porno-thologist

    Just two quick questions

    Horny Porno-thologist, Jan 27, 2004, in forum: XML
    Replies:
    2
    Views:
    398
    Paul Lee
    Jan 27, 2004
  5. JKop
    Replies:
    11
    Views:
    937
Loading...

Share This Page