Game Development Reference
In-Depth Information
int m_num;
explicit BaseClass(int num)
{
m_num = num;
}
};
class SubClassA : public BaseClass
{
public:
explicit SubClassA(void) : BaseClass(1) { }
};
class SubClassB : public BaseClass
{
public:
explicit SubClassB(void) : BaseClass(2) { }
};
class SubClassC : public SubClassA, public SubClassB
{
public:
void Print(void)
{
cout << m_num << endl;
}
};
In this example, the
Print()
function can
'
t even be called because the code won
'
t
get past the compiler. Visual Studio 2010 generates the following error:
error C2385: ambiguous access of
'
m_num
'
The problem is that both
SubClassA
and
SubClassB
inherit the
m_num
member,
so
SubClassC
has two copies of
m_num
, and the compiler doesn
'
t know which one
you
'
re referring to. You could solve the issue by explicitly choosing one like this:
cout << SubClassA::m_num << endl;
Of course you still have the problem of an unused
SubClassB::m_num
variable
floating around just asking for trouble. Someone is bound to accidentally access that
particular
m_num
. This duplication is made even worse when you realize that in our
use case for the actor tree, you
'
d be doubling up on the
PhysicsActor
class. That
means potentially duplicating large objects.