is it possible to return the sizeof a derived class already from base class/struct?
imho the size of a class is a kind of property of itself, like the weight of a human being. But I don’t want to write the same function in every class.
many thanks in advance
Oops
PS: so code to make my question more clear:
template <typename T>
struct StructBase {
size_t size<T>(){
return sizeof(T);
}
};
struct MyStruct: public StructBase<MyStruct> {
double d1;
double d2;
double d3;
MyStruct(): d1(0), d2(0), d3(0){}
//I do not want to do this here
//size_t size(){ //return size<MyStruct> ;}
};
int main(void) {
MyStruct m;
std::cout << m.size(); //nop ?!?
}
You can’t overload the sizeof operator. And I’m not sure what you are asking:
then:
gives you the size of class A – I don’t see where derivation comes into this.
Edit: and regarding your expanded question, I don’t see why you can’t say:
Note that size is not a property like weight – weight can change, and is different for different instances. The size of a class must be known by the compiler at compile time and cannot ever change.