On Feb 27, 8:22 am, Szabolcs Ferenczi <szabolcs.feren...@gmail.com>
wrote:
> On Feb 27, 10:10 am, Kira Yamato <kira...@earthlink.net> wrote:
>
>
>
> > // Is the following code well-defined?
>
> > class A
> > {
> > private:
> > int foo(int n);
>
> > public:
> > typedef int (A::*ftype)(int);
>
> > ftype get() const { return &A::foo; }
>
> > };
>
> > int main()
> > {
> > A a;
> > A::ftype f = a.get();
>
> > (a.*f)(5);
>
> > return 0;
>
> > }
>
> > // Thank you.
>
> > // --
>
> > // kira
>
> It is not the behaviour what is undefined here but the function foo as
> the compiler indicates. g++ gives the following message:
>
> ... In function `A::get() const':
> : undefined reference to `A::foo(int)'
>
> As soon as you define your function foo, it will compile and run.
>
function foo is already defined in the private section(and the post is
about breaking it using function pointer).
> In this code fragment, you just try to demonstrate how one can misuse
> an object-oriented language concept, don't you?
>
> Best Regards,
> Szabolcs
Unfortunately, the same code compiles under g++ 3.4.4(my version of g+
+ on my toshiba tecra 9000 laptop with FreeBSD 6.1). I would say the
code is correct syntatically but not semantically. As pointed out
earlier, we are trying to break the languages rules or using the dark
corners of the language. If interested, google for writing oo code
using 'c'.
Hope someone posts what the standard says. Anyhow, hats off to you.
Thanks,
Balaji.
|