First, you guys are right, Abstract is just a class name, really, the class is inherited (sorry, they mean the same to me in English). |
Do you understand what the difference is? Do you understand what an
abstract class is?
Anyways, one thing I left out was that my vector of class objects is actually a vector of std::shared_ptr's, so does that work just as well as regular pointers for the purpose of casting? |
Yes. Shared pointers have polymorphism, just like C-style plain pointers.
Also, I want to make sure that this cast is indeed the same instance of the object, not a copy, so is using const in the constructor enough to do that? |
Using
const won't make any difference regarding that at all. Casting an object will, effectively, make a new, temporary instance of that object.
If you want to use the same instance of an object, then cast a reference or a pointer to the object, not the object itself, just as you're doing.
Also, why are you passing a reference to a shared pointer into your constructor? That will completely bypass the reference-counting behaviour of the shared pointer, which is the main point of using one in the first place! What are you trying to achieve by using a reference here?
The error that the copy ctor throws is [...]
I think this means that I should also give my Inherited class a copy ctor |
The error message is telling you that you're attempting to invoke a constructor of
Inherited that takes a reference to a shared pointer to
Inherited. That's not a copy constructor, it's just a constructor that takes an argument. The term "copy constructor" has a very specific meaning.
Any textbook, or tutorial on classes, should show you the syntax for creating a constructor that takes an argument. Since you haven't shown us your definition of
Inherited - just an empty code block - we can't possibly know what it might need to do in this specific case.
It seems to me that your design isn't quite right for this. A vector is for holding elements of the same type, that can be treated the same. If you want an entity that contains an item of one type,
Derived*, and then several items of another type,
Inherited*, wouldn't you be better off having a struct/class that contains a
Derived* member, plus a vector of
Inherited*?
One of these things is not like the others, so why try and cram them into the same vector? If one of the objects is a special case, and is going to be used differently from the other objects, then treat it clearly and explicitly as a special case. Then you won't have to jump through hoops trying to cast from a base class to a derived one.