Reputation: 1059
I have base class "Actor" and derived class "Outlaw"
"Outlaw" used to be its own base class and I could easily create a pointer using code:
Outlaw * outlaw = new Outlaw();
Now "Outlaw" inherits base type "Actor"
How would I write the new code to make a pointer to Outlaw class? I'm pretty new to C++ but have a good amount of C experience.
Upvotes: 1
Views: 244
Reputation: 145204
It's exactly the same.
But first of all, maybe you don't need a dynamically allocated instance, maybe you just need a variable. In that case just a declare a variable, because due to speed of everything else in C++, dynamic allocation is costly (everything is relative, dynamic allocation is costly relative to an ordinary variable declaration). E.g.,
Outlaw outlaw; // That's it!
If you do need dynamic allocation, best don't store the pointer raw.
Put it in a smart pointer immediately, e.g. a std::shared_ptr
or a std::unique_ptr
:
std::unique_ptr<Outlaw> outlaw( new Outlaw() );
To use the mentioned smart pointer classes, simply include <memory>
.
Upvotes: 2