boost dynamic_pointer_cast, shared_ptr not working as expected
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:
And here is the output when run:
Am I doing something wrong?
Regards,
JeroMiya
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
nevermind, I see what the problem is:
it should be:
shared_ptr<base> b(new sub());
it should be:
shared_ptr<base> b(new sub());
This post means you could use "boost" for iPhone. But I could not do that so far
. Would you please describe the way/ procedure to use boost library for iPhone application development? Thanks in advance.
. Would you please describe the way/ procedure to use boost library for iPhone application development? Thanks in advance.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| SDL shared_ptr problems | Bearcosta | 6 | 6,048 |
Mar 31, 2011 04:25 AM Last Post: TomorrowPlusX |
|

