I am trying to create my own class (NodeWithMin) as elements for stack in C++, and create a new class (StackWithMin) inheriting it. I think i am ok creating the new stack class, but having some problem initialize a new instance of the new class and use it. Does anybody have a good idea with it? I wrote all classes and main in a single file. Thanks.
#include <stack>
class NodeWithMin{
public:
int value;
int min;
NodeWithMin(int v, int min){
this->value = v;
this->min = min;
}
};
template<class NodeWithMin>
class StackWithMin : stack<NodeWithMin>{
public:
typedef stack<NodeWithMin> super;
void push(int value){
int newMin = min(value, this->min());
super::push(new NodeWithMin(value, newMin));
};
int min(){
if(this->isEmpty()){
return numeric_limits<int>::max();
}else{
super::peek().min;
}
};
};
int main(int argc, const char * argv[])
{
StackWithMin<class NodeWithMin>* ss;
ss = new StackWithMin<class NodeWithMin>();
}