I have a base class A, which does some preliminary stuff. There are 2 subclasses B and C, whose behaviours are slightly different from each other. There is a function in A say foo
, which is almost the same for both B and C, except one little step, which involves a function call to bar
. bar
is virtual in A and has definitions in B and C. The problem is bar
has an extra input in C. How do i handle this in the most acceptable and clean way ?
class A {
public:
A( int a, int b );
~A();
void foo( int a, int b );
virtual void bar( int a, int b );
};
class B : public A {
public:
B( int a, int b );
~B();
void bar( int a, int b );
};
class C: public A {
public:
C( int a, int b, int c );
~C();
void bar( int a, int b, int c );
}
void A::foo( int a, int b )
{
// Some code
bar( a, b );
// Some code
}
A::A( int a, int b )
{
// Some code
foo( a, b );
// Some code
}
The constructor and only the constructor calls foo
which inturn is the only function that calls bar
. I understand functions with a different signature than the base class doesnt override the virtual function in the base class. One way of doing it, is to have c
as an argument to A::foo
and A::bar
also, but i want to avoid doing it, since c
wont make much sense in case of B
. Is there a better way of passing c
cleanly to bar
?
Edit:
To give some context, class A
is a FileReader class which reads from a file, and stores it in a vector<unordered_map<int,int>>
, where every index in the vector corresponds to a record.
For class C
, instead of having just a single vector<unordered_map<int,int>>
, it was decided that it would be better if we had a vector<unordered_map<int,int>>
and a vector<size_t>
, because multiple records in the input, now belong to the same entity. Here every index in the first vector correspond to the entity
and not the record
. And the second vector maps the record
to its corresponding entity
. To find which records belong to which entity, there is an external input in the form of a big structure c
, which maps one field of the record to the entity.
Class B
was originally just class A
itself. But now since there are 2 different behaviors of the similar thing, I decided to create a separate derived class for that, and make A
a template.
B
andC
the virtualbar
method fromA
? Its not becauseB
andC
have a common interface and a user can use the classes polymorphically. What else is it ?override
special identifier to mark such functions. Like for example in theB
class declarebar
asvoid bar(int a, int b) override
. Then the compiler will be able to give you errors when the override declaration doesn't match the parent class virtual function declaration. If you did that onC::bar
you would get such an error, because it doesn't overrideA::bar
. Instead it's an overload. As such it's a totally different function.bar
method is a small part of thefoo
method which happens to differ betweenB
andC
. Everything else thatfoo
does, is the same.B
andC
are pretty much the same thing except for a single internal data structure, which the functionbar
handles.foo
is the same and implemented in the base, then it either callsbar(a,b);
or it callsbar(a,b,c);
. The base class doesnt know about the child classes. There is a deeper problem with your design. I suggest you to read about the template method pattern (en.wikipedia.org/wiki/Template_method_pattern), and for the question it would help if you explain the motivation as this seems to be a xy problem meta.stackexchange.com/questions/66377/what-is-the-xy-problemc
isnt directly owned by the classC
, I was reluctant to do it that way.