Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17825

Questions about constructors, pointers, and smart pointers

$
0
0
So I have some quick questions about constructors and pointers Question on constructors: 1. I'm working on this Sprite class and in general sprites need some kind of texture in order to display to the screen. So I have my class looking like this: class Sprite { public: Sprite(Texture* texture); ~Sprite(); Texture* texture; //Other class stuff } Now in the current state of my Sprite class would I be correct in that I cannot use it as a member in a different class, because it has no no-arg (default) constructor? That the only way this class could be a member is if it was a pointer? class SpriteContainer { public: SpriteContainer(); ~SpriteContainer(); Sprite singleSprite; //**BAD** not allowed because Sprite has no no-arg (default) constructor Sprite* singleSpritePtr; //This is allowed because its a pointer std::vector<Sprite> sprites; //**BAD** not allowed because Sprite has no no-arg (default) constructor std::vector<Sprite*> spites; //This is allowed because its a vector of Sprite pointers } I guess my really problem here is that I struggle with when a variable/class should be a pointer or not 2. How do you make an abstract class where there are no methods that should be pure virtual functions? All the functions of the base class have there implementation, but I also do not want this class to be instantiated //Should not be able to instantiate this class. Should be an Abstract class class BaseClass { public: BaseClass() { x = 0; }; virtual ~BaseClass() { }; int x; void coolMethod() { ++x; } } //Can instantiate this class. coolMethod can be called from this class class DerivedClass : public BaseClass { public: DerivedClass(); ~DerivedClass(); } Questions about smart pointers: 1. I have never used these before, but are smart pointers ok when working with COM objects? Will the smart pointer automatically call a COM object's Release function or should I wrap my COM object and explicitly call Release in the wrapper class' destructor? 2. Lets say there is a case where I wanted to give back the user of some APIs a pointer to a resource, but I also want them to know that they don't have to worry about cleaning it up. EG There is resource loader that will clean up all loaded resources once the program shuts down. The user has to do/worry about nothing. What type of smart pointer should I use in a case like this? Should it be a Shared pointer? Or should I make the resource a Unique pointer and then return to the user a raw pointer?

Viewing all articles
Browse latest Browse all 17825

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>