I am trying to create a generic container (e.g. java code below), which has some restrictions, e.g items bigger than some limit cannot be put in it. Problem is that since T is an unknown type, the comparison function with an integer is reporting an error. How to fix this problem?
Secondly, is there a solution if the code was written in C++?
public class Box<T> {
private T val;
private int max;
public Box (int m, T initval) { max = m; val = initval; }
public T get() { return val; }
public void set(T newval) {
val = newval;
if(newval.toInt() >= max) // error on toInt
System.out.printf("ERR: size too big\n");
}
}
You should have
and