# Get sizeof derived class from the base class?

This topic is 2949 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I currently have a very large project, in which I have 92 classes derived from a single base class.

I'd like to make a simple function to get the size of that object's derived class, but without having to make it pure virtual. If I go the pure virtual route to ask the derived class its size, I'll have at least 92 files I have to change. Is there an easier way?

Here's a simple example:

struct Info{   unsigned int Size;   std::string OtherInfo;};class Base{public:   virtual ~Base() {};   Info GetInfo()   {       Info newInfo;       newInfo.Size = sizeof(*this);  // This won't work, gets size of Base       newInfo.OtherInfo = "Blah";       return newInfo;   }   protected:   Base() {};private:   int Foo;};class Derived{public:   Derived() {};   ~Derived() {};private:   int Bar;}

From within any of the derived classes I would like to be able to call 'GetInfo()' and have it give me the size of the object. Unfortunately the only way I can think of is a GetSize() pure virtual function that I would have to handle in the header of all 92 derived classes.

Off-topic: How can I wrap code so it ends up looking like code on these forums?

[Edited by - lordikon on July 19, 2010 12:52:21 PM]

##### Share on other sites
You could give your base class a pure virtual getSize() method that the derived class must implement to return the proper size.

Also, you want to use the [_source_] tags, not the code tags. :)

P.S. I fail in reading comprehension, sorry. :(((

##### Share on other sites
There is another way (CRTP) but it's no easier. I can think of a couple of hacks, but nothing I'd ever put in real code. You're basically stuck.

##### Share on other sites
92 derived classes? That is an unusually high number. There is no easy way to do this AFAIK, no way that will avoid changing every one of those classes.

##### Share on other sites
Quote:
 Original post by rip-off92 derived classes? That is an unusually high number. There is no easy way to do this AFAIK, no way that will avoid changing every one of those classes.

Yea, it is a component based system. 92 components so far.

Sounds like I may have to go the route of altering all 92 classes.

##### Share on other sites
The OP said:

Quote:
 From within any of the derived classes I would like to be able to call 'GetInfo()' and have it give me the size of the object.

This makes me wonder, if you're already inside the derived class anyway, then why can't you just pass the size of the current derived class to GetInfo() as a parameter?
What do you need to know the size for anyway? Maybe people can suggest an alternative route altogether?

##### Share on other sites
Having a set of macros which all components are required to put in their class is a common approach:

// In FooComponent.h:class FooComponent : public ZQEngine::Component {    ZQ_DEFINE_COMPONENT(FooComponent);public:    ....};// In FooComponent.cpp:ZQ_IMPLEMENT_COMPONENT(FooComponent);

Obviously that's no easier this time, but it'll vastly simplify the next change.

##### Share on other sites
Quote:
Original post by Red Ant
The OP said:

Quote:
 From within any of the derived classes I would like to be able to call 'GetInfo()' and have it give me the size of the object.

This makes me wonder, if you're already inside the derived class anyway, then why can't you just pass the size of the current derived class to GetInfo() as a parameter?
What do you need to know the size for anyway? Maybe people can suggest an alternative route altogether?

I guess I misspoke. The called to GetInfo() would probably be in a common place that was called during function that also called to a virtual method for the derived class. If I make the call to GetInfo() from within the derived class, I'm still stuck modifying all 92 classes.

Either way, it seems like I'll have to modify all derived classes to accomplish this.

1. 1
2. 2
frob
15
3. 3
4. 4
5. 5

• 14
• 13
• 14
• 69
• 22
• ### Forum Statistics

• Total Topics
632138
• Total Posts
3004319

×