1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| #include <iostream>
using namespace std;
class Base {
public:
Base() : ptr(NULL), size(0), saved_index(0) {
}
void do_check() const {
if (!check())
cerr << "<< ERROR : check failed ! >>" << endl;
}
protected:
virtual bool check() const = 0;
protected:
void *ptr;
size_t size, saved_index;
};
typedef enum { ct_none, ct_no_nulls, ct_index_bounds, ct_memory_leaks } check_type_t;
template<int check_type>
class Check : public Base {
protected:
bool check() const;
};
template<>
class Check<ct_none> : public Base {
protected:
bool check() const {
return true;
}
};
template<>
class Check<ct_no_nulls> : public Base {
protected:
bool check() const {
return ptr != NULL;
}
};
template<>
class Check<ct_index_bounds> : public Base {
protected:
bool check() const {
return saved_index < size;
}
};
template<>
class Check<ct_memory_leaks> : public Base {
protected:
bool check() const {
return ptr == NULL;
}
};
class Child1 : public Check<ct_none> {
};
class Child2 : public Check<ct_no_nulls> {
};
class Child3 : public Check<ct_index_bounds>, Check<ct_memory_leaks> {
};
int main() {
Child1 c1;
Child2 c2;
Child3 c3;
c1.do_check();
c2.do_check();
//c3.do_check(); // là ça plante à la compilation
return 0;
} |
Partager