Code:
class Base
{
public:
virtual ~Base() { }
virtual int GetSize() = 0;
}
class A : Base
{
int i;
public:
int GetSize();
}
class B : Base
{
char c;
public:
int GetSize();
}
class C : Base
{
public:
int GetSize();
}
class ....
int SAME_CODE_FOR_ALL_DERIVED::GetSize()
{
return sizeof(*this);
}
I have these classes of different sizes that share the same interface class Base and they also share the function int GetSize(). Is it possible to define all those declarations with one single definition rather than defining them all one by one?
If not, is there a way to achieve what I am attempting to do; get the size of the Derived object through the Base object?
PS: I used the keyword class, but I'm hoping the method recommended will also work with structs.
EDIT #1::
EXAMPLE:
#include <iosteam>
int main()
{
Base *base = new A;
std::cout << base->GetLength();
}
// OUTPUT
// 8 = sizeof(int) + vtable pointer
Code works fine, just want to avoid defining int GetSize() 3+ times, if I could possibly only define it once for all of the subclasses.