"James Kanze" <
[email protected]> wrote in message
--Not exactly the same, but fundamentally the same.
Ah ok sorry misunderstanding.
--The C++ standard does specify significant differences in the
--calling syntax (e.g. in 5.2.2).
Ok so this is the syntaxical rules we must obey, is it not?
It's just syntax. It's arbitrary. And superficial. (Other
languages use different syntax to achieve largely the same
thing. And there have been proposals---which I would
support---to unify the C++ syntax, so that for a member
function, obj.func() and func(obj) have exactly the same
meaning.)
-- Beyond that, however, it
--requires the this pointer to be initialized exactly as if it
--were a reference to an object (except that it is initialized
--with pointer type, and not with reference type). And it
--certainly treats them the same in overload resolution. There's
--no fundamental difference between them.
Now I find what you say here slightly confusing.
You say "beyond that" which I take to mean ..Beyond the definition of the
standard, in your previous sentence.
Beyond simple syntax.
Then you go on about what the standard requires for
initialisation of the this pointer. Surely we must either
speak in terms of the standard or not, you seem to jump in and
out of standards context when it suits you.
I'm speaking purely within the context of the standard here.
The standard describes member functions as behaving
fundamentally like free functions with an additional implicite
argument. And for cause---it very definitely intends that an
implementation which does this be legal.
But ignoring that, what you actually speak about is initialisation of the
this pointer, which I presume is a pointer, yes?
Yes and no. It's certainly not a variable with pointer type,
since it's not an lvalue.
You seem to intepret the this pointer a reference, surely the this pointer
is not required to be converted to a refernece, this seems like an
unneccessarry converion forced upon a compiler i.e :
pointer -> reference -> pointer. (why?)
The semantics of the implicit argument used to initialize the
this pointer are the semantics of a reference, and the this
pointer behaves semantically like a reference: it cannot be
null, and it cannot be reseated. When asked why "this" is not
a reference, Stroustrup says "because references didn't exist
when it was introduced." In other words, if they had, this
would probably be a reference.
Don't get me wrong I understand what you are saying about the
underlying mechanics of a nonvirtual function call, but the
issue here is: Are we allowed to do this as per the C++ std,
which is the general concensus of this group to determine
proper C++ code.
Allowed to do what? The standard is carefully crafted so that
existing implementation techniques would be legal. The most
widespread (the only, as far as I know) technique for
implementing member functions is as a normal function with
an additional implicit argument. The standard is carefully
worded so that this implementation is legal. The result is that
member functions and non member functions are pretty much the
same thing, when viewed from a standards point of view.
--There are other subtle differences: a non-const member function
--can be called on a temporary, where as a non-const reference
--cannot be initialized with a temporary, for example.
I see again you introduce references , this obviously has seem to be a
requirement for you argument.
I don't see the need to have any references when calling a member function,
therefore this continued use of referneces in your demonstrations leads me
to believe there is something wrong.
I introduce references because the implicit argument used to
initialize the this pointer behaves like a reference.
Can you not give an example using simple pointers ?
No, because a simple pointer can be null, and it can be
reseated. This is a pointer for historical reasons, but except
for syntax, it behaves like a reference.
-- But in the
--other thread, you were looking for things that would make
--member functions fundamentally different from non-members.
I'm not looking for something to make them different , I know they are
different. I'm trying to understand why you think it's beneficial to think
of them as the the same.
Because they are fundamentally the same.
One word proves they're different ...virtual.
That's a different issue. Although the standard also describes
virtual functions as being fundamentally the same as any other
function. The only thing that differs is which function is
actually choosen. A sort of overloading, deferred to runtime.
Note that although C++ chooses to limit virtual functions to
members, there's no fundamental reason why this should be the
case.
As below we must respectively assume you are obviously not talking about
virtual functions
--Where as I fail to see anything but syntax differences between
--titi and tata in the following:
-- struct Toto
-- {
-- void titi() const;
-- friend void tata(Toto const&);
-- };
--This is, of course, an extreme case of similarity. But my point
--is that except for syntax, member functions have a series of
--characteristics that can, in specific cases, apply to non member
--functions as well.
But that syntax difference means we are not allowed to do the following, or
does it:
struct ObjT{
void mthd(){};
};
ObjT o;
mthd(o); /*I do not understand this is uncharted waters to me*/
That has been proposed. It was too late for C++98, and for some
reason, the proposal has not be followed up for C++0x. The
basic proposal was that o.mthd() and mthd(o) mean exactly the
same thing. (The proposal was never really studied, so there
could be significant problems with it.)
mthd(&o) /*or is this what you propose?its not a reference I know*/
o.mthd() /*my normal calling syntax*/
I don't know if any of the above is proper code I didn't try to compile
anything, please can you explain what is valid as per the standard.
The only point is that whether you write o.mthd() or mthd(o) is
purely syntax. (IIRC, Ada 95 chose the second form.) It has
nothing to do with what a function fundamentally is or is not.