Distinction between a method and a member function

T

Tim Martin

Hi,

I came across a detail in "C++ Gotchas" by Stephen Dewhurst that confused me
a bit. The author states:

'C++ has no "methods." Java and Smalltalk have methods. When you talk about
an object-oriented design and are feeling particularly pretentious, you may
use the terms "message" and "method," but when you get down to discussing a
C++ implementation of your design, use the terms "function call" and
"member function."'

What he doesn't say is what the difference between a method and a member
function is.

Furthermore, I've looked through the C++ FAQ and the archive of this group,
and neither mention the distinction. Quite a few posts in this group do
seem to use the terms interchangably.

Can anyone explain briefly what the difference is?

Tim
 
S

Sam Holden

Hi,

I came across a detail in "C++ Gotchas" by Stephen Dewhurst that confused me
a bit. The author states:

'C++ has no "methods." Java and Smalltalk have methods. When you talk about
an object-oriented design and are feeling particularly pretentious, you may
use the terms "message" and "method," but when you get down to discussing a
C++ implementation of your design, use the terms "function call" and
"member function."'

What he doesn't say is what the difference between a method and a member
function is.

Furthermore, I've looked through the C++ FAQ and the archive of this group,
and neither mention the distinction. Quite a few posts in this group do
seem to use the terms interchangably.

Can anyone explain briefly what the difference is?

There isn't any. However, in C++ the term is member function. In Java the
term is method. Different language, different lingo.

The C++ standard uses the term member function, and sticking with the
convention used in a domain assists communication.
 
J

Jason

Tim Martin said:
Hi,

I came across a detail in "C++ Gotchas" by Stephen Dewhurst that confused me
a bit. The author states:

'C++ has no "methods." Java and Smalltalk have methods. When you talk about
an object-oriented design and are feeling particularly pretentious, you may
use the terms "message" and "method," but when you get down to discussing a
C++ implementation of your design, use the terms "function call" and
"member function."'

What he doesn't say is what the difference between a method and a member
function is.

Furthermore, I've looked through the C++ FAQ and the archive of this group,
and neither mention the distinction. Quite a few posts in this group do
seem to use the terms interchangably.

Can anyone explain briefly what the difference is?

Tim

The author appears to be pedantic about some detail that does not really
exist. Member functions are often used when talking about C++, but in
reality they are all interchangeable descriptions*, and convey the notion of
some action on an object. It is, basically a question of terminology.

* Unless perhaps, you are talking about the differences between each
languages' embodiment of the general theme; In that case maybe it's useful
to use different terminology consistently.
 
O

osmium

Tim said:
I came across a detail in "C++ Gotchas" by Stephen Dewhurst that confused me
a bit. The author states:

'C++ has no "methods." Java and Smalltalk have methods. When you talk about
an object-oriented design and are feeling particularly pretentious, you may
use the terms "message" and "method," but when you get down to discussing a
C++ implementation of your design, use the terms "function call" and
"member function."'

What he doesn't say is what the difference between a method and a member
function is.

Furthermore, I've looked through the C++ FAQ and the archive of this group,
and neither mention the distinction. Quite a few posts in this group do
seem to use the terms interchangably.

Can anyone explain briefly what the difference is?

As far as I am concerned "method" is Smalltalk jargon and "member function"
is C++ jargon. There is a recent,43 message thread, mostly pointless, on
alt.comp.lang.learn.c-c++. In searching for it I noted a 125 message thread
from a couple years ago. I suspect this was the same tempest in a teapot.
I am using a primitive Microsoft program called Lookout Express so this link
will probably be messed up.

http://www.google.com/groups?hl=en&lr=lang_en&ie=UTF-8&threadm=bi38c9$9dg%
241%40titan.btinternet.com&rnum=1&prev=/groups%3Fq%3Dmember%2Bsmalltalk%2Bgr
oup:alt.comp.lang.learn.c-c%252B%252B%26num%3D20%26hl%3Den%26lr%3Dlang_en%26
ie%3DUTF-8%26sa%3DG%26scoring%3Dd
 
K

Kevin Goodsell

osmium said:
As far as I am concerned "method" is Smalltalk jargon and "member function"
is C++ jargon. There is a recent,43 message thread, mostly pointless, on
alt.comp.lang.learn.c-c++. In searching for it I noted a 125 message thread
from a couple years ago. I suspect this was the same tempest in a teapot.
I am using a primitive Microsoft program called Lookout Express so this link
will probably be messed up.

http://www.google.com/groups?hl=en&lr=lang_en&ie=UTF-8&threadm=bi38c9$9dg%
241%40titan.btinternet.com&rnum=1&prev=/groups%3Fq%3Dmember%2Bsmalltalk%2Bgr
oup:alt.comp.lang.learn.c-c%252B%252B%26num%3D20%26hl%3Den%26lr%3Dlang_en%26
ie%3DUTF-8%26sa%3DG%26scoring%3Dd

You can shorten those Google links a lot:

http://www.google.com/[email protected]

That should do it. Only the "threadm" part is important, or "selm" if
it's a particular post, rather than a complete thread.

-Kevin
 
E

E. Robert Tisdale

Tim said:
I came across a detail in "C++ Gotchas" by Stephen Dewhurst
that confused me a bit. The author states:

'C++ has no "methods." Java and Smalltalk have methods.
When you talk about an object-oriented design
and are feeling particularly pretentious,
you may use the terms "message" and "method"
but when you get down to discussing a C++ implementation
of your design, use the terms "function call" and "member function".'

What he doesn't say is
what the difference between a method and a member function is.

Furthermore, I've looked through the C++ FAQ and the archive of this group,
and neither mention the distinction. Quite a few posts in this group
do seem to use the terms interchangeably.
Can anyone explain briefly what the difference is?

The term *method* is usually reserved for the abstract description.
When the abstract method is implemented,
it is called a procedure, subroutine, function, operator or macro
as is appropriate for the computer programming language in question.
The reference to methods in the context of the Java or Smalltalk
computer programming languages is probably abusive.
 
L

llewelly

Tim Martin said:
Hi,

I came across a detail in "C++ Gotchas" by Stephen Dewhurst that confused me
a bit. The author states:

'C++ has no "methods." Java and Smalltalk have methods. When you talk about
an object-oriented design and are feeling particularly pretentious, you may
use the terms "message" and "method," but when you get down to discussing a
C++ implementation of your design, use the terms "function call" and
"member function."'

What he doesn't say is what the difference between a method and a member
function is.

Furthermore, I've looked through the C++ FAQ and the archive of this group,
and neither mention the distinction. Quite a few posts in this group do
seem to use the terms interchangably.

Can anyone explain briefly what the difference is?

Sometimes the term 'method' is used to indicate any operation on an
object. In C++ many operation should not be member functions,
e.g. operators like +, *, ==, and so on.
 
G

Gary Labowitz

E. Robert Tisdale said:
The term *method* is usually reserved for the abstract description.
When the abstract method is implemented,
it is called a procedure, subroutine, function, operator or macro
as is appropriate for the computer programming language in question.
The reference to methods in the context of the Java or Smalltalk
computer programming languages is probably abusive.

Nah. What is refered to as a member function in C++ IS a method in Java.
Just a different name.
A simple function is not a member of a class. There is no equivalent in
Java.
It's just different names.
 
J

jeffc

Tim Martin said:
Hi,

I came across a detail in "C++ Gotchas" by Stephen Dewhurst that confused me
a bit. The author states:

'C++ has no "methods." Java and Smalltalk have methods. When you talk about
an object-oriented design and are feeling particularly pretentious, you may
use the terms "message" and "method," but when you get down to discussing a
C++ implementation of your design, use the terms "function call" and
"member function."'

What he doesn't say is what the difference between a method and a member
function is.

Frankly, he's the one that sounds a bit pretentious. Assuming you are
writing OO code in C++, then using both the language of OO design and the
language of C++ implementation are valid. It doesn't make sense to be
pedantic and point out every time you make a comment that you're in the "OO
realm" or "C++ realm" at the moment. It's not pretentious at all to say
"message" or "method" when discussing a design implemented in C++. If there
is a difference between "method" and "function" in the context about which
I'm speaking, it would be that "method" is more abstract - in the OO sense -
and "function" is more concrete - in the C++ implementation sense.
 
E

E. Robert Tisdale

jeffc said:
It's not pretentious at all to say
"message" or "method" when discussing a design implemented in C++.

I think that it is pretentious.
 
L

llewelly

E. Robert Tisdale said:
Nonsense!

Problem #0 Uncessary access. For example:

struct my_complex
{
double real()const{return real_;}
double imaginary()const{return imaginary_;}
private:
double real_, imaginary_;
};

With this interface, nonassignment operators need no access, and
therefor should not have it.

Problem #1 Member functions are not compatible with non-member
functions, and the standard C++ library treats member functions as
second class citizens, requiring use of std::mem_fun, or
boost::mem_fn, niether of which correctly handles all cases.

#include<algorithm>

struct my_int
{
my_int():value(0){}
explicit my_int(int value):value(value){}
bool operator>(my_int rhs)const{return value > rhs.value;}
private:
int value;
};

int main()
{
int const size= 10;
my_int a[size];
//... fill a.
std::sort(a, a+size, &my_int::eek:perator>);
}

Does not compile. Rewrite operator> as a non-member:

struct my_int
{
my_int():value(0){}
explicit my_int(int value):value(value){}
friend bool operator>(my_int lhs,my_int rhs){return lhs.value > rhs.value;}
private:
int value;
};

int main()
{
int const size= 10;
my_int a[size];
//... fill a.
std::sort(a, a+size, &operator>); //compiles easily.
}

This example could also be 'fixed' using mem_fun, but others using
binders cannot. Some (but not all!) examples using binders can
be fixed with boost::bind or boost::mem_fn .

Problem #2 Overloading rules do not apply implicit conversions for the
left hand operator.

struct my_int
{
my_int():value(0){}
my_int(int value):value(value){}
my_int operator+(my_int rhs)const{return value + rhs.value;}
private:
int value;
};

int main()
{
my_int a;
int b;
my_int c= a + b; //compiles.
my_int d= b + a; //does not compile.
}

I say it is nonsense to implement operator+ so that a + b compiles but
b + a does not. The work-arounds? (a) do without the implicit
conversion, and (b), implement more overloads of operator+, some
of which *must* be non-members, because the left-hand operand
must be of a type other than my_int.

Problem #3 The oft-posted mistake:

struct my_int
{
my_int operator+(my_int rhs){return value + rhs.value;}
private:
int value;
};

Did you catch it? const, where necessary, more easily remembered, (and
more easily noticed missing) in the parameter list.

There are more problems with member functions, but they are mostly
minor.
 
H

Howie

I just thought:

a method is like a "function" but without a return value.

a function is like a "method" but with a return value.


void ThisIsAMethod(.....);


long ThisIsAFunction(....)


Greetings,
Howie
 
J

jeffc

E. Robert Tisdale said:
I think that it is pretentious.

Then I guess you think discussing anything in OO design terms rather than
C++ terms is "pretentious"? That's silly.
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Howie escribió:
a method is like a "function" but without a return value.

That thing is called "procedure" in languages that makes the difference.

Regards.
 
K

Kevin Goodsell

Howie said:
I just thought:

a method is like a "function" but without a return value.

In some languages that would be a 'procedure'. I've never heard it
called a 'method' before.

In any case, C++ has functions. Whether they return a value or not,
whether they are a member or not, they are always called functions.
That's just the way the language has always been described.

-Kevin
 
W

White Wolf

Tim said:
Hi,

I came across a detail in "C++ Gotchas" by Stephen Dewhurst that
confused me a bit. The author states:

'C++ has no "methods." Java and Smalltalk have methods. When you talk
about an object-oriented design and are feeling particularly
pretentious, you may use the terms "message" and "method," but when
you get down to discussing a C++ implementation of your design, use
the terms "function call" and "member function."'

What he doesn't say is what the difference between a method and a
member function is.

Furthermore, I've looked through the C++ FAQ and the archive of this
group, and neither mention the distinction. Quite a few posts in this
group do seem to use the terms interchangably.

Can anyone explain briefly what the difference is?

The difference is that method is an OO term and there is no well defined
meaning for in when you talk about C++. Some understands it as any member
function. Some others understand it only as virtual member functions. In
fact - if we take the original OO meaning - non-member operator functions
(found by ADL) should also be called methods in C++, since they *are* part
of the classes interface, they just happen to be implemented as a non-member
function for technical reasons.

C++ really has no methods. It is a terminology aspect. Since what methods
mean in a C++ implementation context might be ambiguous it helps if this
term is not used in that context. Of course this is not a law, it is just
common sense: helps to avoid misunderstandings. For example if you ask the
question: what methods does C++ generate automaticly? some might answer:
none. Since while you have meant "member functions" when saying "method"
those answering meant "virtual member functions" when they have read
"methods".
 
R

Ron Natalie

Kevin Goodsell said:
In some languages that would be a 'procedure'. I've never heard it
called a 'method' before.
The common computer science definition of a method is an action performed
on an object. Therefore, they are usually implemented in C++ as non-static
member functions, although not all member functions in C++ are used as methods.

Anyhow, the term is outside the C++ language.
 
S

Shane Beasley

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top