I've a parent class with 2 or more child class deriving from it. The number of different child classes may increase in future as more requirements are presented, but they'll all adhere to base class scheme and will contain few unique methods of their own. Let me present an example -
#include <iostream>
#include <string>
#include <vector>
#include <memory>
class B{
private: int a; int b;
public: B(const int _a, const int _b) : a(_a), b(_b){}
virtual void tell(){ std::cout << "BASE" << std::endl; }
};
class C : public B{
std::string s;
public: C(int _a, int _b, std::string _s) : B(_a, _b), s(_s){}
void tell() override { std::cout << "CHILD C" << std::endl; }
void CFunc() {std::cout << "Can be called only from C" << std::endl;}
};
class D : public B{
double d;
public: D(int _a, int _b, double _d) : B(_a, _b), d(_d){}
void tell() override { std::cout << "CHILD D" << std::endl; }
void DFunc() {std::cout << "Can be called only from D" << std::endl;}
};
int main() {
std::vector<std::unique_ptr<B>> v;
v.push_back(std::make_unique<C>(1,2, "boom"));
v.push_back(std::make_unique<D>(1,2, 44.3));
for(auto &el: v){
el->tell();
}
return 0;
}
In the above example tell()
method would work correctly since it is virtual and overrided properly in child classes. However for now I'm unable to call CFunc()
method and DFunc()
method of their respective classes. So I've two options in my mind -
either packup
CFunc()
and friends inside some already defined virtual method in child class so that it executes together. But I'll loose control over particular execution of unique methods as their number rises.or provide some pure virtual methods in base class, which would be like
void process() = 0
and let them be defined in child classes as they like. Would be probably left emptyvoid process(){}
by some and used by some. But again it doesn't feels right as I've lost return value and arguments along the way. Also like previous option, if there are more methods in some child class, this doesn't feels right way to solve.
and another -
dynamic_cast<>
?. Would that be a nice option here - casting back parent's pointer to child's pointer (btw I'm using smart pointers here, so onlyunique/shared
allowed) and then calling the required function. But how would I differentiate b/w different child classes? Another public member that might return some unique class enum value?
I'm quite unexperienced with this scenario and would like some feedback. How should I approach this problem?
static_cast
could work too. – hg_gitCFunc
andDFunc
? In C++, "I want to do this but it doesn't work well" is often an indicator that this is the wrong solution to your actual problem. – kfsoneon()
,off()
,read()
,write()
and tons of methods simillar, but temp sensor has one diff method -int getTemp()
, as is the case with buzzer -void make_sound()
. I could always put their prototype as pure virtual in base class, but this won't ... – Abhinav GauniyalHardwareInterface
which has only theon()
,off()
,read()
,write()
for all devices. DefineSensorInterface
which has only theget_value()
method (for temperature, light sensor, etc),AlertInterface
with thealert()
method (for buzzer, vibrator, etc). ABuzzer
class will then inherit from theHardwareInterface
andAlertInterface
. Wouldn't this approach work for you? – Nikopol