|
Boost Users : |
Subject: [Boost-users] Determine Parent Class from boost::any
From: Yan Cheng Cheok (yccheok_at_[hidden])
Date: 2009-11-23 11:18:28
May I know is there any way to determine parent class from boost::any?
#include <iostream>
#include <boost/any.hpp>
class a {
public:
virtual ~a() {}
};
class b : public a {
};
bool is_class_a(const boost::any& any)
{
return boost::any_cast<a>(&any) != 0;
}
bool is_class_a_v2(const boost::any& any)
{
try
{
boost::any_cast<a>(any);
return true;
}
catch(const boost::bad_any_cast &)
{
return false;
}
}
int main()
{
std::cout<< is_class_a(b())<< std::endl; // return 0. but i wish to return 1.
std::cout<< is_class_a(a())<< std::endl; // return 1.
std::cout<< is_class_a_v2(b())<< std::endl; // return 0. but i wish to return 1.
std::cout<< is_class_a_v2(a())<< std::endl; // return 1.
}
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net