1

I have this snippet of the code:

void addLineRelative(LineNumber number, LineNumber relativeNumber) { list<shared_ptr<Line> >::iterator i; findLine(i, number); if(i == listOfLines.end()){ throw "LineDoesNotExist"; } line 15 if(dynamic_cast<shared_ptr<FamilyLine> >(*i)){ cout << "Family Line"; } else { throw "Not A Family Line"; } } 

I have class Line and derived from it FamilyLine and RegularLine, so I want find FamilyLine

my program fails on the line 15, I receive an error

cannot dynamic_cast target is not pointer or reference 

can somebody please help, thanks in advance

edited

I tried this one:

shared_ptr<FamilyLine> ptr(dynamic_cast<shared_ptr<FamilyLine> >(*i)); if(ptr){ //do stuff } 

the same error

edited

void addLineRelative(LineNumber number, LineNumber relativeNumber) { list<shared_ptr<Line> >::iterator i; findLine(i, number); if(i == listOfLines.end()){ throw "LineDoesNotExist"; } shared_ptr<FamilyLine> ptr(dynamic_pointer_cast<FamilyLine>(*i)); if (ptr){ cout << "Family Line"; } else { throw "Not A Family Line"; } } 

receive this error

Multiple markers at this line - `dynamic_pointer_cast' was not declared in this scope - unused variable 'dynamic_pointer_cast' - expected primary-expression before '>' token 

1 Answer 1

5

shared_ptr does not implicitly convert to a pointer - it is a class-type object - and dynamic_cast, static_cast and const_cast all operate on pointers only.

While you could use dynamic_cast on shared_ptr<T>::get(), its better to use dynamic_pointer_cast<FamilyLine>() instead as you might otherwise accidentally introduce double-deletes:

Returns:
* When dynamic_cast<T*>(r.get()) returns a nonzero value, a shared_ptr<T> object that stores a copy of it and shares ownership with r;
* Otherwise, an empty shared_ptr<T> object.
[...]
Notes: the seemingly equivalent expression

shared_ptr<T>(dynamic_cast<T*>(r.get())) 

will eventually result in undefined behavior, attempting to delete the same object twice.

E.g.:

shared_ptr<FamilyLine> ptr(dynamic_pointer_cast<FamilyLine>(*i)); if (ptr) { // ... do stuff with ptr } 
Sign up to request clarification or add additional context in comments.

14 Comments

@hello: What version are you using, TR1 et al (use <memory>) or boost (<boost/pointer_cast.hpp>)? (Side-note: The owner of a post is always notified of comments, so the @user syntax isn't neccessary in this case)
Then you are probably using TR1 or C++0x features and #include <memory> should be sufficient.
Well, every C++ library worth its grain either has its types and functions in a namespace (like std for the standard library, boost for Boost, ...) or at least gives them a common prefix (Q for Qt). So as long as you don't have any using directives for boost anywhere but have them for std, the shared_ptr has to come from the standard library or an extension of it. This confusion is by the way a good argument against putting using namespace foo; everywhere.
@hello: Do you have #include <memory>? Do you use any special includes (i.e. besides <memory>) for shared_ptr?
Aha, what is shared_ptr.h? Where does that come from?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.