1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include<iostream>
struct A
{
void foo()
{
pre();
do_foo();
post();
}
protected:
virtual void pre()=0;
virtual void post()=0;
virtual void do_foo()=0;
};
struct B1 : virtual A
{
void pre(){std::cout<<"Pre1"<<std::endl;}
void post(){std::cout<<"post1"<<std::endl;}
};
struct B11 : virtual A
{
void pre(){std::cout<<"Pre2"<<std::endl;}
void post(){std::cout<<"post2"<<std::endl;}
};
struct B2 : virtual A
{
void do_foo()
{
std::cout<<"B2::do_foo"<<std::endl;
}
};
struct B22 : virtual A
{
void do_foo()
{
std::cout<<"B22::do_foo"<<std::endl;
}
};
struct C1 : B1, private B2
{};
struct C2 : B11, private B22
{};
void fun(A& a)
{
a.foo();
}
int main()
{
C1 c1;
C2 c2;
fun(c1);
fun(c2);
} |
Partager