F
fgh.vbn.rty
Say I have a base class B and four derived classes d1, d2, d3, d4. I
have three functions fx, fy, fz such that:
fx should only be called by d1, d2
fy should only be called by d2, d3
fz should only be called by d1, d3, d4
I think I have two options.
(1) Make all functions virtual and define them in the required derived
classes. This will of course lead to a lot of code duplication and
problems in maintainability.
(2) Keep the functions in the base class. Define some type of
identifier (ex: a string) for each of the derived classes and add some
checks at the top of each fx, fy, fz to ensure that only the
appropriate di's are accessing them.
I have implemented (2) but I'm not happy with it because I don't seem
to use the identifier for anything else. Is it possible to use
something like typeid instead for the same purpose?
Is there any other alternate way to do something like this? May be a
design pattern that can be adapted to handle such a situation?
have three functions fx, fy, fz such that:
fx should only be called by d1, d2
fy should only be called by d2, d3
fz should only be called by d1, d3, d4
I think I have two options.
(1) Make all functions virtual and define them in the required derived
classes. This will of course lead to a lot of code duplication and
problems in maintainability.
(2) Keep the functions in the base class. Define some type of
identifier (ex: a string) for each of the derived classes and add some
checks at the top of each fx, fy, fz to ensure that only the
appropriate di's are accessing them.
I have implemented (2) but I'm not happy with it because I don't seem
to use the identifier for anything else. Is it possible to use
something like typeid instead for the same purpose?
Is there any other alternate way to do something like this? May be a
design pattern that can be adapted to handle such a situation?