I am still trying to grip the concept of Abstract Base Classes and what can and cannot be done from the Derived class.
I have the following code :
class NpBaseTest {
...
friend std::ostream& operator<<(std::ostream &os, const NpBaseTest& p_npBaseTest);
/* Implemented in the .cpp file as -
std::ostream& operator<<(std::ostream &os, const NpBaseTest& p_npBaseTest)
{
p_npBaseTest.show(os);
return os;
}
*/
virtual uint16_t process(int) const = 0;
virtual void show(std::ostream& os) const
{
os
<< "Test ID [" << get_test_id() << "] "
<< "Module Type [" << get_module_type() << "]" << std::endl;
}
};
class CavTest : public NpBaseTest
{
...
friend std::ostream& operator<<(std::ostream &os, const CavTest& p_cavtest);
/* Implemented in the .cpp file as
std::ostream& operator<<(std::ostream &os, const CavTest& p_cavtest)
{
p_cavtest.show(os);
return os;
}
*/
uint16_t process(int p_val) const
{
//...implemented here
}
void show(std::ostream& os) const
{
NpBaseTest::show(os);
os
<< "CavTest Type" << std::endl;
}
};
What I am hoping to do in the derived class show function, call the show function from the base class but I get the following error. What I understand is since NpBaseTest is an ABC it doesn't instantiate an object when the Derived class object is instantiated. So what are my options for achieving the desired functionality ?
Error message :
components/cavium/CavTest.cpp: In function 'void show(std::ostream&)': components/cavium/CavTest.cpp:20: error: cannot call member function 'virtual void NpBaseTest::show(std::ostream&) const' without object
UPDATE : Initial issue solved. Looking for more advice Is there a better way to call the base class function without having to specify the base class name with this->::show(os) ?
SOLUTION :
I changed the base class show()
routine to be a non virtual private member function which in turn calls a pure virtual show_all(ostream &os)
function implemented by each derived class. That way I do no have to redefine the operator<<
for every derived class and can avoid using <BaseClassName>::show()
Example in Base Class -
void NpBaseTest::show(std::ostream& os) const
{
//Do base functionality here
os
<< "Test ID [" << get_test_id() << "] "
<< "Module Type [" << get_module_type() << "]" << std::endl;
// Call derived class routine here
show_all(os); //This is declared as virtual void show_all(ostream& os) const = 0;
}
In Derived class
void CavTest::show_all(std::ostream& os) const
{
os
<< "CavTest Type" << std::endl;
}
this->NpBaseTest::show(os);
. But it should actually compile from what you're showing. – πάντα ῥεῖthis->
should be implicit for an inherited virtual function override. – πάντα ῥεῖ