#include <stdio.h>
class CBase
{
public:
virtual void test()
{
printf("CBase::test()\r\n");
}
};
class CDerived1 : public CBase
{
};
class CDerived2 : public CBase
{
};
class CTest : public CDerived1, public CDerived2
{
};
int main()
{
CTest test;
test.test();
return 0;
}
However, the Microsoft VC+ 2005 .NET compiler gives the following message:
1>------ Build started: Project: vft_test, Configuration: Debug Win32 ------
1>Compiling...
1>main_romb.cpp
1>c:\source\vft_test\vft_test\main_romb.cpp(27) : error C2385: ambiguous access of 'test'
1> could be the 'test' in base 'CBase'
1> or could be the 'test' in base 'CBase'
1>c:\source\vft_test\vft_test\main_romb.cpp(27) : error C3861: 'test': identifier not found
1>Build log was saved at "file://c:\Source\vft_test\vft_test\Debug\BuildLog.htm"
1>vft_test - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
#include <stdio.h>
class CBase
{
public:
CBase()
{
printf("CBase::CBase()");
}
virtual void test()
{
printf("CBase::test()\r\n");
}
};
class CDerived1 : public CBase
{
public:
CDerived1()
{
printf("CDerived1::CDerived1()");
}
};
class CDerived2 : public CBase
{
public:
CDerived2()
{
printf("CDerived1::CDerived1()");
}
};
class CTest : public CDerived1, public CDerived2
{
public:
CTest()
{
printf("CTest::CTest()");
}
};
int main()
{
CTest test;
return 0;
}
#include <stdio.h>
class CBase
{
public:
CBase()
{
printf("CBase::CBase()\r\n");
}
virtual void test()
{
printf("CBase::test()\r\n");
}
};
class CDerived1 : virtual public CBase
{
public:
CDerived1()
{
printf("CDerived1::CDerived1()\r\n");
}
};
class CDerived2 : virtual public CBase
{
public:
CDerived2()
{
printf("CDerived2::CDerived1()\r\n");
}
};
class CTest : public CDerived1, public CDerived2
{
public:
CTest()
{
printf("CTest::CTest()\r\n");
}
};
int main()
{
CTest test;
test.test();
return 0;
}
The application output is shown on the following screen shot:
#include <stdio.h>
class CBase
{
public:
virtual void test()
{
printf("CBase::test()\r\n");
}
};
class CDerived1 : public CBase
{
};
class CDerived2 : public CBase
{
};
class CTest : public CDerived1, public CDerived2
{
};
int main()
{
CTest test;
test.CDerived1::test();
return 0;
}
#include <stdio.h>
class CBase
{
public:
virtual void test()
{
printf("CBase::test()\r\n");
}
};
class CDerived1 : public CBase
{
};
class CDerived2 : public CBase
{
};
class CTest
{
CDerived1 m_One;
CDerived2 m_Two;
public:
void test()
{
m_One.test();
m_Two.test();
//or just
//m_One.test();
}
};
int main()
{
CTest test;
test.test();
return 0;
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (3)
Commented:
now I understand. It solves a problem in my current project.
Commented:
You have my vote above.
Commented: