boost dynamic_pointer_cast, shared_ptr not working as expected

Posts: 45
Joined: 2006.11
Post: #1
I am trying to use inheritance and shared_ptr from the boost library. But I'm unable to properly downcast shared_ptr of base classes into shared_ptr of subclasses.

Here is the simplest example I could write:

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <cassert>

using namespace std;

class base {
  virtual ~base() {}

class sub : public base
  virtual ~sub() {}

#define BASE_CAST(x) boost::dynamic_pointer_cast<sub, base>(x)

int main()
  boost::shared_ptr<base> b(new base());
  boost::shared_ptr<sub> s(BASE_CAST(b));
  return 0;

And here is the output when run:
jeremybell@jeremybell-desktop:~$ ./a.out
a.out: test.cpp:25: int main(): Assertion `s.get()' failed.
Aborted (core dumped)

Am I doing something wrong?

Quote this message in a reply
Posts: 45
Joined: 2006.11
Post: #2
nevermind, I see what the problem is:

it should be:

shared_ptr<base> b(new sub());
Quote this message in a reply
Posts: 6
Joined: 2008.11
Post: #3
This post means you could use "boost" for iPhone. But I could not do that so far Sad . Would you please describe the way/ procedure to use boost library for iPhone application development? Thanks in advance.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  SDL shared_ptr problems Bearcosta 6 10,783 Mar 31, 2011 04:25 AM
Last Post: TomorrowPlusX