Base class determining derived classes size?

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 ];
};
Last edited on
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.
Last edited on
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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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.
Last edited on
closed account (o1vk4iN6)
You can use a template if they are all going to be of type float:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

template< int S >
class vec_variables
{
public:
     float u[ S ];
};

template<>
class vec_variables< 1 >
{
public:
     union {
          float x;
          float u[1];
     };
}

template<>
class vec_variables< 2 >
{
public:
     union {
          struct { float x, y; };
          float u[2];
     };
}

template<>
class vec_variables< 3 >
{
public:
     union {
          struct { float x, y, z; };
          float u[3];
     };
}


template< int S >
class vec : public vec_variables< S >
{

     float DotProduct()
     { }

};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.
Exactly what I ended up doing, thanks.
Topic archived. No new replies allowed.