Interfaces and polymorphism producing a compiler warning
Disclaimer: I am a university student in a not-directly programming
related field, although I want to go into programming, so I have not
yet taken any real formal programming courses or training. If my
question seems simple or odd please forgive me! :)
I have been trying to expand my C++ knowledge by wrapping a C++/CLI
(Microsoft extension) class so that it can be used in unmanaged C++.
To my understanding, the only way to accomplish this is to create a
library containing an unmanaged interface to mixed wrapper classes
(i.e. classes containing gcroot<T^> items from vcclr.h). I have been
able to do this without problems; however, while attempting to make
the interface and wrapper classes polymorphic I am receiving compiler
warning that bug me.
Below is a minimal trivial example that illustrates the problem and
contains no C++/CLI (as that would be off-topic in this newsgroup).
The code has been compiled and run in Visual Studio 2008 Express
V9.0.30729.1 under Windows XP. The only warning is =93warning C4250:
'Derived' : inherits 'Base::Base::GetShort' via dominance=94, which can
be solved by including the GetShort() definition that is currently
commented out in the Derived class -- although this does not change
the behaviour of the code. I am hoping that there is a better way to
accomplish this so that I do not have to explicitly forward calls from
the derived functions to the base functions (as with non-trivial code
this would be very repetitive and prone to error). Any additional
nitpicks on my code would also be appreciated!
Thanks in advance,
Franko
class IBase
{
public:
virtual ~IBase() { }
virtual short GetShort() const = 0;
};
class IDerived : public virtual IBase
{
public:
virtual int GetInt() const = 0;
};
class Base : public virtual IBase
{
public:
Base(short s) : m_s(s) { }
virtual short GetShort() const
{
return m_s;
}
protected:
short m_s;
};
class Derived : public virtual IDerived, private virtual Base
{
public:
Derived(int i, short s) : Base(s), m_i(i) { }
virtual int GetInt() const
{
return m_i;
}
//virtual short GetShort() const
//{
// return Base::GetShort();
//}
// warning C4250: 'Derived' : inherits 'Base::Base::GetShort' via
dominance
private:
int m_i;
};
IDerived* CreateDerived(int i, short s)
{
return new Derived(i, s);
}
#include <iostream>
using std::ostream;
using std::cout;
using std::endl;
ostream& operator<<(ostream& os, IBase const * const pBase)
{
return os << pBase->GetShort() << endl;
}
ostream& operator<<(ostream& os, IDerived const * const pDerived)
{
IBase const * const pBase = pDerived;
return os << pDerived->GetInt() << endl << pBase;
}
int main(int argc, char** argv)
{
IDerived* pDerived = CreateDerived(5, 2);
IBase* pBase = pDerived;
cout << pDerived << endl;
cout << pBase << endl;
delete pDerived; // or delete pBase
system("PAUSE");
}