[Rcpp-devel] External pointer to an instance of a C++ class

Darren Cook darren at dcook.org
Thu Sep 15 01:26:31 CEST 2011

> In some way I think this is legitimate in that, IIRC, a pointer to an
> instance of a subclass can be used as a pointer to an instance of the
> parent class.
> So the purpose of this posting is to ask if I am correct that this
> should be expected to work or should I expect that on some exotic
> compilers this will suddenly blow up on me?

(Assuming this was a pure C++ question...) I think that direction of
casting is okay, and part of the point of C++. You can pass a
BigStrongMan to a function that only asks for a Man.

Effective C++ item 39 warns of the dangers of casting code the other way
when you think you know what you are doing. E.g. You have an Man
pointer, but know it is really a pointer to the derived BigStrongMan
class, so you cast it to BigStrongMan* and use it as such.

Item 14 warns to make sure you destructors are virtual if you will be
deleting an instance of BigStrongMan with a Man* pointer. (It is fine to
do this, just make sure the destructors are virtual, so that
BigStrongMan's destructor can be called.)


Darren Cook, Software Researcher/Developer

http://dcook.org/work/ (About me and my work)
http://dcook.org/blogs.html (My blogs and articles)

More information about the Rcpp-devel mailing list