Topic: Scope resolution doesn't work with virtual functions
Author: schnitker@sigma-c.com (Uwe Schnitker)
Date: Tue, 19 Nov 2002 18:07:49 +0000 (UTC) Raw View
news@ono.com (?ISO-8859-15?Q?Arnau Rossell Castell ?) wrote in message news:<P16A9.2835$l84.252136@news.ono.com>...
> I've come across a problem that i've distilled
I'd like all people could distill their problems equally well.
> in this code snippet:
>
> #include<iostream>
>
> struct A
> {
> virtual int f1(void){return 0;}
> int f2(void){return 0;}
> };
>
> struct B: public A
> {
> virtual int f1(void){return 1;}
> int f2(void){return 1;}
> };
>
> int main(int argc, char* argv[])
> {
> B test;
> int(B::*p1)(void),(B::*p2)(void);
> p1=B::f1;p2=B::f2;
> std::cout << (test.*p1)() << "\t"<<(test.*p2)() << "\n";
> std::cout << test.f1() << "\t"<<test.f2() << "\n\n";
> p1=B::A::f1;p2=B::A::f2;
> std::cout << (test.*p1)()
Please note that while this piece doesn't work as you expect,
> << "\t" << (test.*p2)() << "\n";
> std::cout << test.A::f1()
this piece of code does.
><< "\t" << test.A::f2() << std::endl;
>
> return 0;
> }
>
> I thought that this program should output something like:
> 1 1
> 1 1
>
> 0 0
> 0 0
>
> But instead it does this:
> 1 1
> 1 1
>
> 1 0
> 0 0
>
> I'm using microsoft visual c++ 7(.net).
> I'd like to know if this is standard behavior, or the compiler is
> wrong(or i've messed something important).
You should have named the subject:
" ... doesn't work with pointer-to-members to virtual functions"
And now you should understand: When using pointers or pointer-to-members,
you get polymorphic behavior with virtual functions.
With virtual functions, a pointer-to-member can only point to a dynamically
bound function, and the actually called function is determined by the actual
type of the object on which its called.
When you call the function without a pointer-to-member - test.A::f1() -
everything works like you expected.
>
> Thank you in advance.
>
> Arnau Rossell Castell
HTH,
Uwe
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: news@ono.com (=?ISO-8859-15?Q?Arnau_Rossell=F3_Castell=F3?=)
Date: Tue, 12 Nov 2002 12:39:14 +0000 (UTC) Raw View
I've come across a problem that i've distilled in this code snippet:
#include<iostream>
struct A
{
virtual int f1(void){return 0;}
int f2(void){return 0;}
};
struct B: public A
{
virtual int f1(void){return 1;}
int f2(void){return 1;}
};
int main(int argc, char* argv[])
{
B test;
int(B::*p1)(void),(B::*p2)(void);
p1=3DB::f1;p2=3DB::f2;
std::cout << (test.*p1)() << "\t"<<(test.*p2)() << "\n";
std::cout << test.f1() << "\t"<<test.f2() << "\n\n";
p1=3DB::A::f1;p2=3DB::A::f2;
std::cout << (test.*p1)() << "\t" << (test.*p2)() << "\n";
std::cout << test.A::f1() << "\t" << test.A::f2() << std::endl;
return 0;
}
I thought that this program should output something like:
1 1
1 1
0 0
0 0
But instead it does this:
1 1
1 1
1 0
0 0
I'm using microsoft visual c++ 7(.net).
I'd like to know if this is standard behavior, or the compiler is=20
wrong(or i've messed something important).
Thank you in advance.
Arnau Rossell=F3 Castell=F3
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]