If I defined an interface in a base class and I’m calling that interface through a derived class, the only way that I found to use that base’s class interface is by using “using” in the derived class. I’m not sure if it’s a good practice. Here is a sample program
#include <memory>
#include <iostream>
using namespace std;
template <class T>
class B
{
public:
void outB() { cout << "dim from B is " << dim_ << endl ; }
B(int dim) : dim_(dim) {}
~B() {}
protected:
int dim_;
};
template <class T>
class A : B<T>
{
public:
using B<T>::dim_;
using B<T>::outB;
void outA() { cout << "dim from A is " << dim_ << endl ; }
A(int dim) : B<T>(dim) {}
~A() {}
private:
};
int main(int argc, const char *argv[])
{
shared_ptr<A<double> > ap(new A<double>(2));
ap->outA();
ap->outB();
return 0;
}
If I take away the line using B<T>::outB; the program does not compile. http://ideone.com/x6gm0
The problem with using using all the time is that if I have a long chain of derived classes (for example if I extensively use adapter pattern), I have to write using ...; in each derived class for all of inherited interfaces.
What is a good practice to go around the repetition of using in each derived class to keep the interfaces of the base class?
Note, i’d like to avoid using virtual. I need performance from the code.
Why not declaring
class A: public B<T>in which case you wont need theusing? Now this works out of the box for member functions in the base class. To get access to the base class member variables, you either need to do what you are doing or access them via explicit call tothispointer i.ethis->dim_instead of justdim_