-, :
- ,
IList Queue<T> .Queue<T>,- ,
void push const T& operator[], . IList , , Queue<T> .
- : , , . , T Queue<T>, .
, ( , ), - , -. ++ , dynamic_cast
, ( ) -method ++.
, / - ++ . , , , : . , , . , , , .
T , , , Queue<T>.
, : dynamic_cast. , this , , .
: , . std::string , std::string, , , . , .
, . Parent, Child<T>, , T Child<T>, Parent:
class Parent{
public:
template <typename T>
void foo(const T& t);
virtual ~Parent(){}
};
template <typename T>
class Child : public Parent{
public:
void foo(const T& t);
};
template <typename T>
void Parent::foo(const T& t){
auto castThis = dynamic_cast<Child<T>&>(*this);
castThis.foo(t);
}
template<typename T>
void Child<T>::foo(const T& t){
std::cout << typeid(T).name() << ": " << t << '\n';
}
int main(){
Parent&& handle = Child<int>();
try{
handle.foo<int>(3);
handle.foo<char>(0);
handle.foo<std::string>("Hello!");
}
catch(std::bad_cast e){
std::cout << "bad cast caught\n";
}
}
g++ 5.2.0, clang 3.7
i: 3
bad cast caught
, .
, , . - std::vector<std::unique_ptr<Parent>> , .
, , :
- . , - , .
dynamic_cast , . std::bad_cast. . - - -, - , - name ++. :
, , , . . .
, foo Child<T>, - Child<T>, Parent .
3. , , .