No runtime performance limitations (if optimized). Obviously, a debug build can store some inheritance data).
As evidence, observe the output of the following programs
prog1:
template <class T>
class PointerA
{
public:
PointerA(T * obj) : m_pointer(obj) {}
T & operator*() { return *this->m_pointer; }
T *m_pointer;
};
PROG2:
template <class T>
class Pointer_Impl
{
public:
T & operator*() { return *this->m_pointer; }
protected:
T *m_pointer;
Pointer_Impl(T *) {}
};
template <class T>
class PointerA : public Pointer_Impl<T>
{
public:
PointerA(T * obj) : Pointer_Impl<T>(obj) {}
};
general core:
int main() {
PointerA<int> p(new int);
volatile int i = 42;
*p = i;
i = *p;
}
Both produce the same assembly:
main:
subq $24, %rsp
movl $4, %edi
call operator new(unsigned long)
movl $42, 12(%rsp)
movl 12(%rsp), %eax
movl %eax, 12(%rsp)
movl $0, %eax
addq $24, %rsp
ret
source
share