P
Paul
You said yourself:James Kanze said:news:0e0dc254-eccb-42aa-8ab7-35db4c4218e1@w21g2000yqm.googlegroups.com...
[...]I don't quite follow you here. If I have a function:
void f(int&);
, it's undefined behavior for me to call f without a an object
of type int.
You must've forgot we are talking about *member functions*.
So what's the difference, with regards to your argument.The difference is that the standard defines a rule for nonstatic member
functions. The same rule does not apply to normal functions.
The only difference defined by the standard is that nonstatic
member functions have an implicit first parameter, of type T&
(or T const&).
"A member function has a special calling syntax, and special access rights
to member data".
Also:
A member function must be called on an object , or it produces UB, a normal
function does not have this requirement.
A member function is declared in a class scope, a normal function is not.
What does a reference have to do with anything?How is that different from calling any other function which
takes a reference without an object that the function refers to?
Because the standard lays down some rules specific to NSMF's.Why? It obeys exactly the same rules as a static member
function, or a free function.
A nonstatic member function has different rules than a static member
function or ordinary function.
It most certainly is the same function. The standard requires
it.
The local variables,parameter and return value in function1 are not the same
as those in function2, they are two seperate processes .
Plus you have access rights to member data.
To what do you refer when you suggest the standard requires them to be the
same?
ok so with:An implementation is certainly allowed to make as many copies of
the function as it wishes. Member or otherwise. On the other
hand, the standard requires that all pointers to the function
compare equal. Member or otherwise. Even if the implementation
makes many "copies" of the function (as it typically does if the
function is inline), it has to ensure that all of the pointers
to the function compare equal.
This is very close, if not identical, with the OO concept of
identity. In C++, if two addresses (of objects) compare equal,
they are the same object. Comparison of address is the way you
test identity in C++.
#include <iostream>
class Base1{public: virtual void foo()=0; virtual void bar()=0;};
class Derived: public Base1{
public:
void foo(){std::cout<<"in foo"<< std::endl;}
void bar(){std::cout<<"in bar"<< std::endl;}
};
int main()
{
void (Base1::*fp1)() =&Base1::foo;
void (Base1::*fp2)() =&Base1::bar;
Base1* b1=new Derived;
std::cout<< "fp1 points to: " << fp1 << std::endl;
std::cout<< "fp2 points to: " << fp2 << std::endl;
(b1->*fp1)();
(b1->*fp2)();
}
Does this not prove the address can be the same but the functions are
different?