My question is about member function pointer. Here a sample code: class C
inherits foo
from A
and bar
from B
. I expected both &C::foo
and &C::bar
are of the same type void (C::*)()
, but actually they are different types, one is void(A::*)()
and the other is void(B::*)()
. I used gdb ptype command to inspected data types.
My question is: is this behavior defined by C++ standards? What is the rationale of this design decision?
#include <type_traits>
#include <iostream>
struct A {
void foo() {
std::cout << this << std::endl;
}
int a;
};
struct B {
void bar() {
std::cout << this << std::endl;
}
int b;
};
struct C : public A, B { };
int main() {
auto p1 = &C::foo; // p1 is void (A::*)();
auto p2 = &C::bar; // p2 is void (B::*)();
auto p3 = &A::foo; // p3 is void (A::*)();
bool b1 = std::is_same<decltype(p1), decltype(p2)>::value;
bool b2 = std::is_same<decltype(p1), decltype(p3)>::value;
std::cout << b1 << std::endl; // false
std::cout << b2 << std::endl; // true
return 0;
}
Aucun commentaire:
Enregistrer un commentaire