class Data
{
public:
int i;
};
auto cmp = [](const Data& d1, const Data& d2) { return d1.i > d2.i; };
class A
{
private:
queue<Data> q;
public:
A() {};
void func() {
int cnt = 0;
while (!q.empty()) {
std::cout << cnt++ << std::endl;
q.pop();
}
}
};
class B
{
private:
priority_queue<Data, vector<Data>, decltype(cmp)> q;
public:
B() :q(cmp) {};
void func() {
int cnt = 0;
while (!q.empty()) {
std::cout << cnt++ << std::endl;
q.pop();
}
}
};
I define two classes A and B.As seen, their member func
is the same, but with different member variable type q
and different constructor.
So could I make A and B into two class derived from one base class (but with func
in base class) or make them into a template class?(That is to say, I only want to write func
once..)
If could, then how?