B
Bilgehan.Balban
Hi,
I am currently brushing up my c++ knowledge and I would like to ask you
about the differences between classes and C structs, in the
function/method perspective.
1) Is it correct to say that, a structure definition that includes
function pointers only defines the function prototypes to be used with
them, but not the actual implementations, whereas in C++, member
functions cannot be changed *unless* virtual functions are used, or the
original class is extended with a new one that defines
the same functions with different implementations?
2) Once defined, accessing member functions of a class in C++ has less
overhead thanks to name mangling, compared to the extra pointer
dereference overhead
in C structs with function pointers?
3) It is commonly said that virtual functions reduce performance.
AFAIK, the overhead of virtual functions compared to a function pointer
in a C struct is, only one extra pointer dereference and an addition.
E.g. one dereference to access vtab, and one offset addition and
dereference to access the function itself.
Is it even more than that, otherwise why does virtual functions have
such reputation to reduce performance where C structs with function
pointers are not that much better?
4) Also, isn't there the possibility to optimise the dereference in C
structs, at least if the function pointer is declared `const', i.e. not
to change after the initialisation. The compiler might just recognise
the const initialisation and replace it with a `branch' to the actual
function? Perhaps not a possible option with virtual functions due to
dynamic binding?
5) In a C struct on a 32-bit machine, the size of a struct is the sum
of all fields + maybe some padding; the function pointers take up as
much as a word. In C++ what is the size of a class, perhaps it excludes
sizes of member methods? Is it only the fields? What about static
storage? Does a static member count in every instance of the class? And
const members? Is it implementation defined where consts are stored? If
I wanted to memcpy a class, how much space should I spare? Perhaps it
is undefined behaviour if one manually copied a class and expected to
access its fields as normal?
Many thanks,
Bahadir
I am currently brushing up my c++ knowledge and I would like to ask you
about the differences between classes and C structs, in the
function/method perspective.
1) Is it correct to say that, a structure definition that includes
function pointers only defines the function prototypes to be used with
them, but not the actual implementations, whereas in C++, member
functions cannot be changed *unless* virtual functions are used, or the
original class is extended with a new one that defines
the same functions with different implementations?
2) Once defined, accessing member functions of a class in C++ has less
overhead thanks to name mangling, compared to the extra pointer
dereference overhead
in C structs with function pointers?
3) It is commonly said that virtual functions reduce performance.
AFAIK, the overhead of virtual functions compared to a function pointer
in a C struct is, only one extra pointer dereference and an addition.
E.g. one dereference to access vtab, and one offset addition and
dereference to access the function itself.
Is it even more than that, otherwise why does virtual functions have
such reputation to reduce performance where C structs with function
pointers are not that much better?
4) Also, isn't there the possibility to optimise the dereference in C
structs, at least if the function pointer is declared `const', i.e. not
to change after the initialisation. The compiler might just recognise
the const initialisation and replace it with a `branch' to the actual
function? Perhaps not a possible option with virtual functions due to
dynamic binding?
5) In a C struct on a 32-bit machine, the size of a struct is the sum
of all fields + maybe some padding; the function pointers take up as
much as a word. In C++ what is the size of a class, perhaps it excludes
sizes of member methods? Is it only the fields? What about static
storage? Does a static member count in every instance of the class? And
const members? Is it implementation defined where consts are stored? If
I wanted to memcpy a class, how much space should I spare? Perhaps it
is undefined behaviour if one manually copied a class and expected to
access its fields as normal?
Many thanks,
Bahadir