Thanks,
Luigi
-------------
#include <iostream>
#include <functional>
class Foo {
public:
virtual ~Foo() {}
virtual double bar(double x) const = 0;
double test(double x) const {
double (Foo::*f)(double) const = &Foo::bar;
return std::bind1st(std::mem_fun(f),this)(x);
}
};
class Bar : public Foo {
public:
double bar(double x) const { return x/2.0; }
};
class Baz : public Foo {
public:
double bar(double x) const { return x*2.0; }
};
int main() {
std::cout << Bar().test(42.0) << std::endl;
std::cout << Baz().test(42.0) << std::endl;
return 0;
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
{ Edits: quoted clc++m banner removed. Please don't quote extraneous
material. -mod }
Hi,
Getting the address of a pure virtual function is valid in all
cases. Whenever the address of a virtual/pure virtual function is
taken the vtable is referred and correct address is retrieved. Hence
it is always valid what you have done and it must be compatible across
any c++ compilers.
Thanks,
Amal P
Thank you. May anybody point me to where this is stated in the
standard?
(Not that I distrust you, of course. It's just to further learn my way
about it.)
Luigi
I doubt that anyone will be able to provide such a citation - because
nowhere in the C++ Standard is there any language that states that a
member pointer to a pure virtual method is OK. But - and more
importantly - nowhere in the C++ Standard is there any language that
states that a member pointer to a pure virtual method is not OK.
The C++ Standard would have to prohibit member pointers to pure
virtual methods explicitly (just as the Standard does, for example,
with member pointers to static member functions) in order to make such
member pointers illegal. Otherwise, in the absence of any language to
that effect, member pointers to pure virtual methods must be legal -
and must behave no differently than any other kind of member pointer.
Greg
{ Edits: quoted signature and clc++m banner removed. Please don't quote
extraneous material. -mod }
Hi,
Section 8.3.3, sub index 3 represents the invalid cases of a pointer
to member. All other cases except specially mentioned are valid use.
So go ahead and use it. It is safe.
Amal