T
toton
I have a code like this
typedef unsigned int uint;
class PtuInfo{
bool increasing_;
uint ptIndex_;
uint begin_;
uint end_;
public:
PtuInfo(uint begin,uint end,bool back):
increasing_(!back),
begin_(begin),end_(end){
if(increasing_)
ptIndex_ = begin_;
else
ptIndex_ = end_;
}
void next(){
if(increasing_)
ptIndex_++;
else
ptIndex_--;
}
bool isEnd(){
if(increasing_){
return ptIndex_ == end_;
}
else{
return ptIndex_ == begin_;
}
}
};
Now, as it can be seen, the operations next() & isEnd() can be
statically evaluated, as the increasing_ field is provided during
construction of the object.
Thus it can be split into two class, which dont need to check
increasing_ parameter on each call.
I feel a better way to do it using a template param of bool type, to
avoid code duplication (the class do some other things also, but all
operations are based on the field increasing_ which is passed during
construction only).
Can anyone show a static template equivalent of the above code ?
Thanks in advance
abir
typedef unsigned int uint;
class PtuInfo{
bool increasing_;
uint ptIndex_;
uint begin_;
uint end_;
public:
PtuInfo(uint begin,uint end,bool back):
increasing_(!back),
begin_(begin),end_(end){
if(increasing_)
ptIndex_ = begin_;
else
ptIndex_ = end_;
}
void next(){
if(increasing_)
ptIndex_++;
else
ptIndex_--;
}
bool isEnd(){
if(increasing_){
return ptIndex_ == end_;
}
else{
return ptIndex_ == begin_;
}
}
};
Now, as it can be seen, the operations next() & isEnd() can be
statically evaluated, as the increasing_ field is provided during
construction of the object.
Thus it can be split into two class, which dont need to check
increasing_ parameter on each call.
I feel a better way to do it using a template param of bool type, to
avoid code duplication (the class do some other things also, but all
operations are based on the field increasing_ which is passed during
construction only).
Can anyone show a static template equivalent of the above code ?
Thanks in advance
abir