[[ blog 이사 과정에서 정확한 posting날짜가 분실됨. 년도와 분기 정도는 맞지 않을까? ]]
See following example codes.
class A
{
public:
virtual int a(void) {return 1; }
virtual int b(void) {return 11;}
int c(void) {return 21;}
};
class B
{
public:
virtual int a(void) {return 2; }
virtual int b(void) {return 12;}
int c(void) {return 22;}
};
class C : public A, public B
{
public:
virtual int a(void) { return 3; }
};
void main(void)
{
C c;
A* pa = &c;
B* pb = &c;
printf("%d / %d / %d\n", pa->a(), pb->a(), c.a()); // OK ---(*1)
printf("%d\n", c.b()); // Compile Error due to ambiguous access ---(*2)
printf("%d\n", c.c()); // Compile Error due to ambiguous access ---(*3)
}
In case of (*2) and (*3), Compiler gives 'Error' due to ambiguity - A::b()/B::b(). It's clear.
But, there is no ambiguity in case (*1). Function 'a' is declared as 'virtual' in A and B. And 'a' also overridden in C. So, all three parameters of 'printf' means 'C::a()'.
'Language > C&C++' 카테고리의 다른 글
| [C/C++] Using 'virtual' and 'inline' keywords simultaneously. (0) | 2007.04.04 |
|---|---|
| [C/C++] virtual inheritance (0) | 2007.03.24 |
| [C/C++] Remind! '>>' extends sign bit (0) | 2007.01.11 |
| [C/C++][Tip] Using long and important 'enum'.. (0) | 2006.12.03 |
| [C/C++][Tip] Macro... (0) | 2006.11.17 |