c++ - Memory allocation of instances of class - should inheritance be used to reduce memory consumption? -
is memory allocated class instances not @ dependent on member variables initialized? save memory creating new class less variables if instances don't use variable?
for example:
class animal{ public: int numberoffeet; int averageweight; }; class dogs - public animal { public: char breed; }; int main(){ animal snake1; };
will save memory having 2 classes above, rather having 1 class 3 different properties?
class wrapper, size of class equal sum of size of members in common. exception empty class should not have size of zero.
for questions:
is memory allocated class instances not @ dependent on member variables initialized? save memory creating new class less variables if instances don't use variable?
no, compiler not know whether use variables or not. garbage value if not use them, still value.
will save memory having 2 classes above, rather having 1 class 3 different properties?
no, class dog inherited class animal, have animal has, in case numberoffeet
, averageweight
, owns members, in case breed
. memory not reduce because not use 2 numberoffeet
, averageweight
that when 2 classes depend on other (dog
depends on animal
). if separated (no inheritance), answer should yes (since dog
not contain numberoffeet
, averageweight
).
edited: not time size of class equal sum of member variables.
Comments
Post a Comment