// The name of this class is not a typo. Anything that inherits
// similar, but this seemed like the least clunky alternative.
// some helpers...
template <typename T>
bool is_a(const valueable * pv) {
return (0 != dynamic_cast<const value<T> *> (pv));
}
template <typename T>
const T & item(const valueable * pv) throw(std::bad_cast) {
const value<T> & rT = dynamic_cast<const value<T> &>(*pv);
return rT.item();
}
// derived class, finally!
template <typename T>
class value : public valueable {
public:
value() : val_() {}
value(const value<T> & v) : val_(v.item()) {}
value(const T & v) : val_(v) {}
value(const std::string & s) throw(bad_input);
virtual ~value();
T const & item(void) const { return val_; }
value<T> & operator=(const value<T> & v);
virtual std::string to_string() const;
virtual const std::vector<unsigned char> to_bytes(void) const {
// etc. etc. etc.
}
virtual unsigned int bytes(void) const {
// etc. etc. etc.
}
private:
T val_;
};
I also have a class that implements what amounts to a function call by taking a const reference to an instance of std::vector<const valueable *> as parameters, performs some validation on them, and returns a pointer to a newly allocated valueable instance as a return value. Sounds simple enough, right?
The weird thing is that after I construct this vector of pointers, and attempt a dynamic_cast via the item() or is_a() template functions above on any of the pointers, dynamic_cast returns 0. (For the record, the function call logic is in a shared library, not that this should matter.) I checked that dynamic_cast succeeds when I create the vector. And even stranger is that the output of typeid().name() looks fine (or is at least consistent) as well.
So is there any reason why would dynamic_cast would fail?
Thanks in advance,
-- Dan Caugherty