1a: , ++ ( , , Python). , argv [1].
1b: , .
2: dynamic_cast typeid ( ) , ( , , , ) ( ).
2a: , , , - , , , , . , , , 2a, .
2b: , , :
struct Base {
virtual ~Base() {}
friend std::ostream& operator<<(std::ostream& s, Base const& v) {
v._print(s);
return s;
}
private:
virtual void _print(std::ostream&) const = 0;
};
template<class T>
struct Value : Base {
T data;
explicit
Value(T const& data) : data(data) {}
private:
virtual void _print(std::ostream& s) const {
s << data;
}
};
:
int main(int argc, char** argv) {
using namespace std;
auto_ptr<Base> p;
string const type = argc > 1 ? argv[1] : "int";
if (type == "int") {
p.reset(new Value<int>(2));
}
else if (type == "float") {
p.reset(new Value<double>(2.2));
}
cout << *p << '\n';
return 0;
}
, , Base, . , , boost.variant, , .
Roger Pate