I have the following struct inheritance:
struct Base
{
Base(int a, int b)
: m_a(a), m_b(b)
{
// empty
}
int m_a;
int m_b;
};
struct ChildOne: public Base
{
ChildOne(int a, int b)
: Base(a, b)
{
// empty
}
};
struct ChildTwo: public Base
{
ChildTwo(int a, int b)
: Base(a, b)
{
// empty
}
};
If I want to have an overloaded function, one that handles the case when a ChildOne struct is passed, and one for ChildTwo, how would I implement that?
What I'm currently trying: In hpp:
class MyClass
{
void foo(Base s);
}
In cpp:
void Myclass::foo(ChildOne s)
{
//Do things specific for ChildOne
}
void MyClass::foo(ChildTwo s)
{
//Do things specific for ChildTwo
}
If I understand inheritance correctly, ChildOne and ChildTwo are both Base. I should be able to pass ChildOne to this foo function because ChildOne IS a Base. But this doesn't compile for me, saying it can't find a find a matching prototype in MyClass (but it does recognize foo(Base) as a potential candidate). What am I misunderstanding about overloading functions and inheritance?
note: The reason I have a base class with empty children inheriting it was so I could have a std::queue<Base>
that would only hold ChildOne or ChildTwo structs, no Base structs. This works beautifully. It's calling an overloaded function to handle the front item of the queue (whether it's a ChildOne or ChildTwo) that's the problem.