473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

quick questions

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?

Sep 11 '06 #1
5 1448
"Steve Chow" <ms*********@hotmail.comwrites:
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
Sep 11 '06 #2
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
Sep 11 '06 #3
animal.sounds().make_noise("fart");
is there a name for calling multiple functions like this?
explicit lyrics?
Sep 11 '06 #4
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
Sep 11 '06 #5

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

Sep 11 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.