N00b's programming question
-
The story is I’m acutally making a FLHook plugin.
In the plugin, I have a group of class (Let’s say, derived class) which shared same Base class. And a Manager class used to control and send event one by one to those derived classes with method call.
Code like this:
https://gist.github.com/raincious/6b786a9c7f9e4fa6c0b3My question is:
1, Can it be done? Notice I each derived class can have different size and method, And the Base class may have unvirtual methods.
2, When I try to delete, will be memory which assigned to derived classes can be completely release (memory size is different to Base class)?I’m new to C++, so new …. that I can’t know if I truly need a shared_ptr or not …
Please help, Thank you!
-
If your Base class has at least one virtual member function, make the base class destructor virtual. The result is undefined when you delete a pointer of a base class, pointing to one of the derived classes.
Also for iterators its better to have a pre increment rather than a post increment.
And use initializer lists for classes rather than to assignment in the constructors body.
-
Huor wrote:
If your Base class has at least one virtual member function, make the base class destructor virtual. The result is undefined when you delete a pointer of a base class, pointing to one of the derived classes.Also for iterators its better to have a pre increment rather than a post increment.
And use initializer lists for classes rather than to assignment in the constructors body.
OK, now I have virtual destructor and pre increment iterator (I found this after readed your reply: http://stackoverflow.com/questions/1303899/performance-difference-between-iterator-and-iterator).
So I still have Undefined Behaviour problems even after ‘correct’ delete? How to solve that problem? Or I can only go with smart pointer (The shared_ptr)?
-
you should not have, make sure you have a virtual destructor in all upper base classes available, so when you have derived1 and derived2 and derived inherits from derived1 then you also need a virtual destructor in derived1 and so on and so forth.
I am not so familiar with shared_prt but in principal you could use them too. But even shared_ptr wont solve the problem of eventual non-virtual destructors. It only takes over destructing if you forget to destruct your objects (as far as i know).
-
Naked pointers is asking for trouble. Get a C++11 compliant compiler (VS2012 and up) and use unique_ptr.
-
FriendlyFire wrote:
Naked pointers is asking for trouble. Get a C++11 compliant compiler (VS2012 and up) and use unique_ptr.Wow, I learned a lot today LOL.
Yeah, I think unique_ptr is better since I don’t need copy them.
And looks I also don’t need to care about class slicing because memory will be erased by internal magic stuffs itself. (Is I’m right?)
OK, I will change code and go with smart pointer.
Thank you FF.
-
Yeah, if you use a unique_ptr the memory will be released and the destructors called as soon as the pointer gets out of scope. This will be the case even if there’s an exception somewhere, whereas an explicit delete call might be skipped due to an exception.