boost dynamic_pointer_cast, shared_ptr not working as expected

Member
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:

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

using namespace std;

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

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

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


int main()
{
  boost::shared_ptr<base> b(new base());
  assert(b.get());
  boost::shared_ptr<sub> s(BASE_CAST(b));
  assert(s.get());
  return 0;
}

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

Am I doing something wrong?

Regards,
JeroMiya
Quote this message in a reply
Member
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
Apprentice
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 7,361 Mar 31, 2011 04:25 AM
Last Post: TomorrowPlusX