• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
C++: what could cause dynamic_cast<> to fail?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

C++: what could cause dynamic_cast<> to fail?


  • Subject: C++: what could cause dynamic_cast<> to fail?
  • From: Dan Caugherty <email@hidden>
  • Date: Sat, 23 Jan 2010 16:01:04 -0500

Hey all --

It may offend purists, but I have a legitimate reason to use RTTI. (Or at least I haven't found a good way to avoid it.) But for whatever reason, dynamic_cast<> keeps failing me.

Let me explain; I have the following code in place:

template <typename T> class value; //forward declaration

// The name of this class is not a typo.  Anything that inherits
// this interface acts like a value<> type (defined below). 
// I could have called this class value_interface or something 
// similar, but this seemed like the least clunky alternative.

class valueable {
public:
  valueable();
  virtual ~valueable();
  virtual const std::vector<unsigned char> to_bytes(void) const = 0;
  virtual std::string to_string() const = 0;
  virtual unsigned int bytes(void) const = 0;
};

//  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


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: C++: what could cause dynamic_cast<> to fail?
      • From: Jens Alfke <email@hidden>
  • Prev by Date: Xcode 3.2.1 Debug target malloc problem with Boost Libraries 1.41
  • Next by Date: gdb: Newbie can't use 'print -[object message]'
  • Previous by thread: Xcode 3.2.1 Debug target malloc problem with Boost Libraries 1.41
  • Next by thread: Re: C++: what could cause dynamic_cast<> to fail?
  • Index(es):
    • Date
    • Thread