To better explain this problem, I built a simple example. Let's say I have a Blob class as follows:
class Blob { string personalName; string& familyName; }
A Blob can be spawned by the Creator (aka Programmer), after which he gets the choice of personalName , and since he has the privilege of being the 1st generation of Blob , he gets his own familyName .
Alternatively, a Blob can be created by creating an existing Blob , after which it selects its own personalName , but shares a familyName with all the other Blob that have been cloned into this family. If one Blob changes the name, all other family members automatically change this name.
While this sounds good and good, until the Blob constructor is written, I see this:
Blob::Blob() : personalName(pickName()), familyName(pickFamilyName()) { } ... string& Blob::pickFamilyName() { return *(new string("George")); }
Ik! Allocating memory on the heap and then assigning it a reference variable ?! It looks scary!
Are my instincts correct that something is wrong with this, or does it just seem strange to me, because this is not a common template? If something is wrong, what is it? Why is this a bad design?
Note. It would be important to free the memory allocated by the heap by counting and deleting the memory when deleting the last Blob or in some other way.
Cory klein
source share