473,411 Members | 1,880 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

shared_ptr and containers

Hi!

I try to do the following:

class A {
};

class B : public A {

};

int main()
{
vector< shared_ptr<A v;
shared_ptr<Bd( new B );
v.push_back( d );
shared_ptr<Bd2 = dynamic_pointer_cast( v[0] );
}

and the compiler (g++) bails out with an:

error: no matching function for call to
'dynamic_pointer_cast(boost::shared_ptr<A>&)'
Is dynamic_pointer_cast supposed to work with containers like vector?

Thanks.

Sep 17 '07 #1
1 1990
Found out myself;

(...)
dynamic_pointer_cast<B>
(...)

instead of

(...)
dynamic_pointer_cast
(...)

did it.
Also, I had to add a virtual destructor in "A" to make it polymorphic.

Sep 17 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Markus Dehmann | last post by:
The main difference between std::auto_ptr and boost::shared_ptr is that auto_ptr has the transfer-of-ownership semantic. That makes it impossible to store auto_ptrs in STL containers because the...
14
by: PengYu.UT | last post by:
In the following program, I want an iterator contain pointer pointing to constant object not const pointer. If it is possible would you please let me know how to do it? #include...
6
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
2
by: g | last post by:
Hello, Wouldn't be better shared_ptr constructor to have an argument <true/falsefor thread-safe reference counting?? The default should be true and in case you dont want thread-safe RC create a...
3
by: Tim H | last post by:
I'm newish to C++ but not to C. I'm confused by this code. test1() is fine. test2() fails to compile. /tmp/inherit_ptr.cpp: In function âvoid test2()â: /tmp/inherit_ptr.cpp:52: error: no...
9
by: Tim H | last post by:
Why is the following code not valid? I mean, I see the code and it doesn't allow it, but I am curious about the rationale? boost::shared_ptr<intpi = new int; pi = new int; Thanks Tim
4
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
5
by: Fokko Beekhof | last post by:
Hello all, please consider the following code: -------------------------------------------------- #include <tr1/memory> struct BaseA { int x;
5
by: .rhavin grobert | last post by:
are there any pro's and con's in using references to shared_ptr<>'s ? example: __________________________________________________ void obj::foo(shared_ptr<>); vs. void...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.