V
Vikram
I was just curious to know how are calls like say dynamic_cast
implemented. Is it really expensive to get the exact type of an object
being pointed to by the baseclass pointer?
The reason I am asking is because of the well known issue of pointer
arithmetic not going well with polymorphism. Consider a function like
void myFunc( baseC ptr[], int length) {
for (int i=0; i<length; i++)
ptr.someField = somevalue;
}
Here baseC is some base class. Now, this function will not work as
intended if I pass in an array of derived class objects. (because
ptr gets mapped to (ptr + i*sizeof(baseC)) )
Is it really expensive for the Compiler to generate proper code here
rather than just the sizeof addition? By proper code, I mean get the
type info for the actual objects pointed at by ptr and use that size.
If it indeed is that expensive, can it just do it for cases like above
where I can specifically give some kind of hint that myFunc can
"possibly" be invoked with an array of derived class objects.
Thanks,
Vikram
implemented. Is it really expensive to get the exact type of an object
being pointed to by the baseclass pointer?
The reason I am asking is because of the well known issue of pointer
arithmetic not going well with polymorphism. Consider a function like
void myFunc( baseC ptr[], int length) {
for (int i=0; i<length; i++)
ptr.someField = somevalue;
}
Here baseC is some base class. Now, this function will not work as
intended if I pass in an array of derived class objects. (because
ptr gets mapped to (ptr + i*sizeof(baseC)) )
Is it really expensive for the Compiler to generate proper code here
rather than just the sizeof addition? By proper code, I mean get the
type info for the actual objects pointed at by ptr and use that size.
If it indeed is that expensive, can it just do it for cases like above
where I can specifically give some kind of hint that myFunc can
"possibly" be invoked with an array of derived class objects.
Thanks,
Vikram