I understand that you are saying that calling a function with an explicit
(reference to an object ) argument, is the same as calling a member function
with hidden argument.
Yeah. That's what I meant.
That I think it is beneficial to think of nonstatic member functions
this way. for example:
void A::foo( Par1, Par2 );
as a function of arity 2, with a first parameter of type A&, that is
special in the following properties:
- in the call syntax it is written before the function arg0.foo( arg1,
arg2 )
- the lookup for the function name is done only by that special
argument's type, and not by all the other (unlike global functions).
So the difference with global functions or static member function is
only in the call syntax and lookup rules, but nothing else.
* Except for that weird temporary bind to non-const reference
difference. I think they probabely should all been disallowed.
That is, such a function is equivalent to:
void foo( A&, Par1, Par2 );
foo( arg0, arg1, arg2 );
I think keeping in mind this equivalence would be beneficial for your
subjects of arguments' validity and any definition of existance.
I am a bit cautious to agree because I think there may be cases where this
is not always true. Perhaps some calling conventions differ and also I think
with virtuals the case is slightly different.
I'm not sure what you mean by "calling conventions".
However, the virtual case is more delicate to express but is not
different. There's still no inherent difference with global functions,
because:
In theory virtuality is a property of any certain parameter of a
function. I.e a few parameters of a certain function can be virtual -
such functions are called "multimethods". And again, theoretically it
does not matter if they are global, static members or nonstatic
members (that just changes the syntax). There are languages that have
this feature.
Although Stroustrup wanted to add this feature to c++ already before
1990, as he says in "The Design and Evolution of C++", the main reason
we don't have them in c++ by now, is not enough time to work on that,
due to other more important features they had to add.
I do believe it will be added one day to the language, though
probabely not even by c++1X.
This is a quite complicated feature to implement for a language. It
has been decided to restrict the virtuality feature in a way that
allows implementation of it to be much simpler, and that required:
- only 1 parameter of a function is allowed to be virtual
- any override of a function that is virtual on a certain parameter,
must be declared within the class type of this parameter. practically
then, it was very convenient to call them "virtual member functions",
and use the same syntax rules as nonstatic member functions.
And when we need multi-virtuality we use workaround idioms like multi-
dispatch.
For example, the multi-virtuality feature could look like:
class Carnivour {};
class Prey{};
void eat_prey( virtual Carnivour&, virtual Prey& ) = 0;
//overrides
class Lion: public Carnivour {};
class Anaconda: public Carnivour {};
class Bear: public Carnivour {};
class Girrafe: public Prey {};
class Gazelle: public Prey {};
void catch_prey( Lion&, Gazelle& ) { ... } //jumps on it and bite its
neck
void catch_prey( Lion&, Girrafe& ) { ... } //bite its ass
void catch_prey( Cobra&, Gazelle& ) { ... } //inject venom
//Anaconda can't kill girrafes so no override for that one
void catch_prey( Bear&, Prey& ) { ... } //because Bears catch
everything in the same way lol.
But putting those cases aside
I think you are correct to say that
afunct(&aObj) ;
cannot exist unless aObj exists.
I didn't say that it exists.
I say: if we define the meaning of "function exists" in a certain way,
then it should turn out the same for nonstatic member function as for
any reference parameter in any type of function.
But I don't know any definition for "function exists" and I don't know
what beneficial usage could be done with such definition.
itaj