Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find size of derived class object using base class pointer

Tags:

Is it possible to find the size of a derived class object using a base class pointer, when you don't know the derived type.

Thank you.

like image 990
Chenna V Avatar asked Oct 04 '11 02:10

Chenna V


People also ask

How do you determine the size of a derived class?

@John5342 - sizeof(DerivedClass1) must include BaseClass::a and BaseClass::b . Those members must exist. The fact that their relative positions may be different in any class that inherits from DerivedClass1 doesn't change that. sizeof(DerivedClass1) includes all members, even those inherited via virtual inheritance.

Can a base class pointer point to a derived class object?

Explanation: A base class pointer can point to a derived class object, but we can only access base class member or virtual functions using the base class pointer because object slicing happens when a derived class object is assigned to a base class object.

How do you calculate the size of an object in a class?

One way to get an estimate of an object's size in Java is to use getObjectSize(Object) method of the Instrumentation interface introduced in Java 5. As we could see in Javadoc documentation, the method provides “implementation-specific approximation” of the specified object's size.

Can a base class pointer call methods in the derived class?

When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.


2 Answers

There's no direct way, but you can write a virtual size() method child classes can implement. An intermediary templates class can automate the leg work.

struct base {
  virtual size_t size() const =0;
  virtual ~base() { }
};

template<typename T> 
struct intermediate : base {
  virtual size_t size() const { return sizeof(T); }
};

struct derived : intermediate<derived> 
{ };

This does require your hierarchy be polymorphic... however, requesting behavior based on the dynamic type of an object rather than its static type is part of the definition of polymorphic behavior. So this won't add a v-table to the average use case, since at the very least you probably already have a virtual destructor.

This particular implementation does limit your inheritance tree to a single level without getting into multiple inheritance [ie, a type derived from derived will not get its own override of size]. There is a slightly more complex variant that gets around that.

struct base { /*as before */ };

template<typename Derived, typename Base>
struct intermediate : Base {
  virtual size_t size() const { return sizeof(Derived); }
};

struct derived : intermediate<derived, base>
{ };

struct further_derived : intermediate<further_derived, derived>
{ };

Basically, this inserts an intermediate in between each actual layer of your hierarchy, each overriding size with the appropriate behavior, and deriving from the actual base type. Repeat ad nauseum.

//what you want
base >> derived 
     >> more_deriveder
     >> most_derivedest

//what you get
base >> intermediate<derived, base> 
     >> derived >> intermediate<more_deriveder, derived> 
     >> more_deriveder >> intermediate<most_derivedest, more_deriveder> 
     >> most_derivedest

Several mixin-type libraries make use of such a scheme, such that the mixins can be added to an existing hierarchy without introducing multiple inheritance. Personally, I rarely use more than a single level of inheritance, so I don't bother with the added complexity, but your mileage may vary.

like image 194
Dennis Zickefoose Avatar answered Sep 19 '22 11:09

Dennis Zickefoose


I don't think it can be done, because sizeof works on compile time types. You could define a virtual Size function in the base class and override it for each derived class.

like image 35
Mark Ransom Avatar answered Sep 21 '22 11:09

Mark Ransom