Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > quick questions

Reply
Thread Tools

quick questions

 
 
Steve Chow
Guest
Posts: n/a
 
      09-11-2006
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?

 
Reply With Quote
 
 
 
 
Jens Theisen
Guest
Posts: n/a
 
      09-11-2006
"Steve Chow" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Stuart Redmann
Guest
Posts: n/a
 
      09-11-2006
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
 
Reply With Quote
 
Gernot Frisch
Guest
Posts: n/a
 
      09-11-2006

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


explicit lyrics?


 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      09-11-2006
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
 
Reply With Quote
 
tragomaskhalos
Guest
Posts: n/a
 
      09-11-2006

Steve Chow wrote:
>
> animal.make_noise("fart");
>
> but the other day i ran into something like
>
> animal.sounds().make_noise("fart");
>


Note that there is a school of thought that says that the second form
is A Bad Thing: see http://en.wikipedia.org/wiki/Law_Of_Demeter or
http://www.c2.com/cgi/wiki?LawOfDemeter

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding quick-release to a non-quick-release tripod head ste7esmith@hotmail.com Digital Photography 4 11-20-2006 03:19 PM
Quick question, hopefully quick answer. ~misfit~ NZ Computing 114 01-06-2005 01:36 PM
Quick Question Quick Answer JKop C++ 11 05-24-2004 09:46 PM
Quick Restore for a Compaq not so quick! Croos Bustamunky Computer Support 2 05-15-2004 04:17 AM
PanasonicBQ390 "quick" charger - How quick? Ol' Bab Digital Photography 1 01-17-2004 06:54 AM



Advertisments