Есть виртуальные функции и указатели.
C++ |
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
| #include <string>
#include <sstream>
#include <iostream>
class Exception {
public:
virtual std::string what() const = 0;
private:
};
template <class T>
class SpecialException : public Exception {
public:
SpecialException(const T &value) : data(value) {}
std::string what() const {
std::stringstream result;
result << data;
return result.str();
}
private:
const T data;
};
template <class T>
class A {
public:
A(const T &value) { throw new SpecialException<T>(value); }
};
int main(int argc, char *argv[]) {
try {
A<int> a(12345);
} catch (Exception *e) {
std::cout << e->what() << std::endl;
delete e;
}
try {
A<std::string> a("Text message.");
} catch (Exception *e) {
std::cout << e->what() << std::endl;
delete e;
}
} |
|