Obviously if you have a function that calls sizeof( this ) on any class it will return the number of bytes that class uses based on how many member variables exist in it. Well I want to create several classes that all use the same functions but contain different amounts of member variables, therefor the size won't be constant and that size is necessary for some of the functions I'm using (math related). I could add a SetSize( ) to BaseClass but then I would have to add a member variable to that class which would effect the size of all the classes it inherits.
I would much prefer to get the size of the derived class (DerivedClass / TestClass) through the inherited class (BaseClass) but I'm not entirely sure how to go about it, if it's even possible. I would really prefer not to have a ton of polymorphic functions that essentially do the exact same thing but work on different amounts of variables. Anyone have any other ideas to avoid this? Please let me know if I didn't explain my question well enough for you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class BaseClass
{
int GetDerivedSize( )
{
// Should return 0x20 for DerivedClass and 0x40 for FurtherClass
return ( sizeof( this ) );
}
};
class DerivedClass : public BaseClass
{
char szPadding[ 0x20 ];
};
class FurtherClass : public BaseClass
{
char szPadding[ 0x40 ];
};
Well first of all, sizeof(this) would give you the size of a pointer (likely 4 or 8), not the size of the class. Since this is a pointer.
And secondly, the size of polymorphic types is borderline useless because it will include things like vtables which are inconsistent across implementations.
So I'm forced to ask why you are trying to do this in the first place?
EDIT:
Also, if the type is not polymorphic, there is no way for the base class to know what type of child class it is referring to, and therefore it is impossible for it to determine the size. The only way for the base class to know the size in that case would be to store the size in member variable and have the derived class assign it.
Yea, I know, it was an example and I just didn't think to dereference it. Basically I want to set up an array of classes that handle different numbers of floats and perform math operations on them. Rather than setting up, for example, two classes that have TWO floats and THREE floats and performing the exact same operations, I figured I would just derive a class that handles all that stuff and that way I could dynamically create as many new classes as I want without recreating functions and bulking up my source code. The only problem is that I need to know the number of floats from the base class being inherited so that I can apply it to all other classes. Another, hopefully more clear example, of what I am trying to accomplish...
class BaseClass
{
int GetDerivedSize( )
{
// I want sizeof( *this ) to be the size of the class that
// inherits BaseClass if that wasn't obvious enough :P
return ( sizeof( *this ) / sizeof( float ) );
}
float DotProduct( )
{
FLOAT Dot = 0.f;
// Obviously this won't work if I don't have the proper size
for ( int i = 0; i < GetDerivedSize( ); i ++ )
Dot += ( ( float* )this )[ i ] * ( ( float* )this )[ i ];
return Dot;
}
};
class F3 : public BaseClass
{
float A, B, C;
};
class F4 : public BaseClass
{
float A, B, C, D;
};
class F5 : public BaseClass
{
float A, B, C, D, E;
};
class F6 : public BaseClass
{
float A, B, C, D, E, F;
};
PS: I've thought of a few simpler ways to do this anyway, was just a spur of the moment thought I had to trim down my code. However, I still am curious how to go about something like that if it's even possible.
class BaseClass
{
int GetDerivedSize( )
{
// I want sizeof( *this ) to be the size of the class that
// inherits BaseClass if that wasn't obvious enough :P
return ( sizeof( *this ) / sizeof( float ) );
}
float DotProduct( )
{
FLOAT Dot = 0.f;
// Obviously this won't work if I don't have the proper size
for ( int i = 0; i < GetDerivedSize( ); i ++ )
Dot += ( ( float* )this )[ i ] * ( ( float* )this )[ i ];
return Dot;
}
};
Be careful, using GetDerivedSize for a BaseClass will result in a 0/32. Don't remember if it's 0 or a error.