made it pointer, because then i have to worry about the allocation and deallocation of the memory.
2. If I did make it a pointer (and use the compiler defaults), i should define all the 6 special member function explictly, because the compiler generated defaults may not be accurate in case of pointer members.
Right?
1- That's RAII, allocate your resource in the ctor and release it in the dtor. It's fine and depending upon your class design a pointer or non-pointer may be needed. But the simpler the better and if you can tune out of pointers that would be more recommended as they're quite error prone 'But' if you need them go and use them as they're quite strong. You can also you smart pointers if you're too worried about deallocating the allocated memory.
Yes, this is the gist of the matter. Pointers come with the extra cognitive load of ownership; who owns this resource right now, and who is eventually responsible for it's safe disposable after use. This requires special and critical attention. If you really need the member as a pointer after much analysis, consider one of the myriad of smart pointers the C++ world offers, in and out of the standard. Those raw pointers should really be avoided, except if you are implementing some unique specialized resource handling facility. As for the six special member functions, they have the interesting interaction in which defining one can disable the others, and thus throw your expectations off course. Bermuda Triangle stuff. Hence, if you define one, define them all, for proper and expected operation of your facility, and also for your own sanity.
Обсуждают сегодня