Re: Is it legal code?
"James Kanze" <james.kanze@gmail.com> wrote in message
news:5dcb68a6-5205-408c-a99f-79afe41ed0b1@s18g2000vbe.googlegroups.com...
On Feb 22, 1:55 am, "Paul" <pchris...@yahoo.co.uk> wrote:
"James Kanze" <james.ka...@gmail.com> wrote in message
news:b4306fba-68dd-44e6-a035-c42a6b44b514@4g2000yqo.googlegroups.com...
On Feb 21, 12:46 pm, "Paul" <pchris...@yahoo.co.uk> wrote:
"James Kanze" <james.ka...@gmail.com> wrote in message
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&).
You said yourself:
"A member function has a special calling syntax, and special access
rights
to member data".
Friend functions also have special access rights. And what if C++
had adopted a unified calling syntax (like IIRC Ada does)?
I don't see how syntax could make a difference.
With regards to all comments about member functions being the same as normal
function please use my other post on that subject.
<big snip>
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++.
ok so with:
#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?
How have you defined << for a pointer to member function.
Without any additional code, this shouldn't compile. (Curiously
enough, it does with both g++ and VC++. But in both cases, the
output is more or less arbitrary, and reading it back in won't
give you the same pointer.) Try adding
std::cout << "fp1 == fp2: " << (fp1 == fp2 ? "true" : "false") <<
std::endl;
to your code, and see what it says. If it says "true", your
compiler is not conformant. (Also, be wary of VC++ here. By
default, it's handling of pointer to member functions is broken.
You need the option /vmg for it to work. Not that it changes
anything here.)
--
Sorry I started playing around and the code got changed. I mean this:
#include <iostream>
class Base1{public: virtual void foo()=0; };
class Base2{public: virtual void foo()=0; };
class Derived: public Base1, public Base2{
public:
void foo(){std::cout<<"in foo"<< std::endl;}
};
int main()
{
void (Base1::*fp1)() =&Base1::foo;
void (Base2::*fp2)() =&Base2::foo;
Base1* b1 = new Derived;
Base2* b2 = new Derived;
(b1->*fp1)();
(b2->*fp2)();
}
Two addresses one function?
BTW Java sun docs are good references , I will look at that other post
later , when I have time.