diff options
author | Santo Cariotti <dcariotti24@gmail.com> | 2020-02-04 17:20:17 +0100 |
---|---|---|
committer | Santo Cariotti <dcariotti24@gmail.com> | 2020-02-04 17:20:17 +0100 |
commit | 0e26332bfb379a946c4774aa96d81e110071abb5 (patch) | |
tree | 1d056f5eced3dabe69597b8959054816bfb25000 /I_anno | |
parent | adf21e477a0f0cb5a934933468f170aa2e49cb30 (diff) |
fix: improve template security with its specialization
Diffstat (limited to 'I_anno')
-rw-r--r-- | I_anno/Programmazione_1/lab_14_12_18.cc | 41 |
1 files changed, 32 insertions, 9 deletions
diff --git a/I_anno/Programmazione_1/lab_14_12_18.cc b/I_anno/Programmazione_1/lab_14_12_18.cc index 527da64..02bbd69 100644 --- a/I_anno/Programmazione_1/lab_14_12_18.cc +++ b/I_anno/Programmazione_1/lab_14_12_18.cc @@ -58,31 +58,54 @@ private: }; template<typename T> -class C : public A { +class C; + +template<> +class C<double> : public A { public: C(short n) : A{n} { - if(typeid(T) == typeid(short)) { - x = g(n); - } else { - x = static_cast<double>(log(1+n)); - } + x = static_cast<double>(log(1+n)); + } + + double foo(short r) const { + return g(r*x); + } + + double g(double k) const { + return 3*k; + } + + void print(ostream& os) { + os << "C<double> "; + getPtr(os); + os << ", x=" << x; + } +private: + short x; +}; + +template<> +class C<int> : public A { +public: + C(short n) : A{n} { + x = g(n); } double foo(short r) const { return g(r*x); } - T g(T k) const { + short g(short k) const { return 3*k; } void print(ostream& os) { - os << "C<" << typeid(T).name() << "> "; + os << "C<short> "; getPtr(os); os << ", x=" << x; } private: - T x; + short x; }; ostream& operator<<(ostream& os, A& a) { |