【C++】template 統合スレ -- STL/Boost/Loki, etc.
■ このスレッドは過去ログ倉庫に格納されています
0056デフォルトの名無しさん
NGNGclass A {
public:
int i;
};
class B : public A {
public:
int j;
B(int x, int y) { i = x; j = y; }
};
template<class T, class U> std::auto_ptr<T>
inline cast(std::auto_ptr<U>& ref)
{
return std::auto_ptr<T>(std::auto_ptr<U>(ref));
}
int main()
{
std::auto_ptr<B> ptr(new B(123, 456));
std::auto_ptr<A> pa;
pa = cast<A>(ptr);
std::cout << pa->i << std::endl;
ptr = cast<B>(pa); // ダウンキャストはだめ?
std::cout << ptr->j << std::endl;
}
■ このスレッドは過去ログ倉庫に格納されています