My application uses a large number of Panda objects. Each Panda has a list of Bamboo objects. This list does not change after Panda initialization ( Bamboo objects are added or deleted). Currently, my class is implemented as follows:
class Panda { int a; int b; int _bambooCount; Bamboo* _bamboo; Panda (int count, Bamboo* bamboo) { _bambooCount = count; _bamboo = new Bamboo[count];
To ease the overhead of allocating an array of Bamboo objects, I could implement this class as follows: basically, instead of creating objects through a regular constructor, the construction method allocates one memory block for storage like Panda and its Bamboo array:
class Panda { int a; int b; Panda () {
Can you anticipate any problems with the second design besides increasing the maintenance effort? Is this an acceptable design pattern or just a premature optimization that could bite me again?
c ++ design-patterns data-structures class-design
Tony the pony
source share