Consider the following invalid C++ code.
#include <assert.h>
class NodeInterface {
public:
virtual ~NodeInterface () {}
virtual int f (const int& n) const = 0;
};
class ChildNodeInterface : public NodeInterface {
public:
virtual ~ChildNodeInterface () {}
};
class ParentNodeInterface : public NodeInterface {
public:
virtual ~ParentNodeInterface () {}
};
class ChildNode : public ChildNodeInterface {
public:
virtual ~ChildNode () {}
virtual int f (const int& n) const {
return 2*n;
}
};
class ParentNode : public ParentNodeInterface, private ChildNodeInterface {
public:
explicit ParentNode () :
mChild (new ChildNode ())
{
}
virtual ~ParentNode () {}
ChildNodeInterface* GetChildHandle () {
return this;
}
virtual int f (const int& n) const {
return 3*n;
}
private:
ChildNode* const mChild;
// How do I specify that I would like to override ChildNodeInterface::f?
virtual int f (const int& n) const { // On MSVC2010: C2535 member function already defined or declared
return 1 + mChild->f (n);
}
};
int main()
{
ParentNode parent;
assert (parent.f (2) == 6);
ChildNode node;
assert (node.f (2) == 4);
ChildNodeInterface* child (parent.GetChildHandle ());
assert (child->f (2) == 5);
return 0;
}
It is my aim to make ParentNode privately look like a ChildNode, so that it can add some additional functionality on top of ChildNode‘s implementation of ChildNodeInterface. ParentNode could thereby effectively be treated as a ChildNode-handle in disguise, indicated by the simplicity of GetChildHandle.
Clearly, if ParentNode would not repeatedly inherit from NodeInterface, there would be no problem. Since, one can then easily disambiguate the overrides. This is illustrated by the following correct example:
#include <assert.h>
class ChildNodeInterface {
public:
virtual ~ChildNodeInterface () {}
virtual int ChildMethod (const int& n) const = 0;
};
class ParentNodeInterface {
public:
virtual ~ParentNodeInterface () {}
virtual int ParentMethod (const int& n) const = 0;
};
class ChildNode : public ChildNodeInterface {
public:
virtual ~ChildNode () {}
virtual int ChildMethod (const int& n) const {
return 2*n;
}
};
class ParentNode : public ParentNodeInterface, private ChildNodeInterface {
public:
explicit ParentNode () :
mChild (new ChildNode ()),
mValue (1)
{
}
ChildNodeInterface* GetChildHandle () {
return this;
}
virtual int ParentMethod (const int& n) const {
return 3*n;
}
private:
ChildNode* const mChild;
const int mValue;
virtual int ChildMethod (const int& n) const {
return mValue + mChild->ChildMethod (n);
}
};
int main()
{
ParentNode parent;
assert (parent.ParentMethod (2) == 6);
ChildNode node;
assert (node.ChildMethod (2) == 4);
ChildNodeInterface* child (parent.GetChildHandle ());
assert (child->ChildMethod (2) == 5);
return 0;
}
However, in the special case where ParentNodeInterface and ChildNodeInterface both inherit from NodeInterface, the ambiguity arises.
As should be clear from the assertions in main, I do not aim for virtual inheritance of NodeInterface. It is my intention to have truly distinct implementations of NodeInterface::f in ParentNode.
I wonder how (if possible) I can discriminate between the implementation of ParentNodeInterface::f and ChildNodeInterface::f in ParentNode.
I think your design is wrong. You are inherited ChildNodeInterface and you have a ChildNode member in your ParentNode Class?
Inherited ChildNodeInterface doesn’t allow you to use f function implementation since ChildNode class is not in the inheritance tree.
Redifining twice a function with the same signature is not allowed by C++. So writing 2 implementation of the same virtual function can’t be done.
So you have 2 options:
[Fastest] Remove inheritance from ChildNodeInterface and prefer calling mChild->f() in the ParentNode::f() function to add the Child behavior to the parent:
virtual int f (const int& n) const
{
return 3*n + mChild->f(n);
}
But I don’t know if it’s the behavior you want.