It depends on the hardware platform you are compiling to, but the layout is usually pretty similar across implementations. After all, the first C++ was CFRONT, which compiled C++ to C...
The platform-dependent issues and the memory layouts will be described in a "platform C++ ABI" where ABI stands for "Application Binary Interface."
struct Cxx_ABI_Header
{
unsigned inheritance_backward_offset; /* Must be Zero for base object */
unsigned rtti; /* Each class has its own signature. */
void * vtable; /* Pointer to array of virtual function pointers. */
}
struct object_one
{
char * file_name;
int file_descriptor;
}
int object_one_create_file(struct object_one *);
int object_one_delete_file(struct object_one *);
int object_one_update_file(struct object_one *, off_t offset,
size_t nbytes_replace, size_t nbytes_supplied,
char * buf);
int object_one_read_file(struct object_one *, off_t offset,
size_t nbytes_read, char * buf);
int object_one_op_noauthz(struct object_one *)
{
return ENOACCESS;
}
void * CRUD_vtable_authenticated_user = {
{ object_one_create_file, object_one_read_file,
object_one_update_file, object_one_delete_file }};
void * CRUD_vtable_guest = {
{ object_one_op_noauthz, object_one_read_file,
object_one_op_noauthz, object_one_op_noauthz }};
Here is a possible constructor, which actually makes two different kinds of "object_one".
struct object_one * new_object_one(char * filespec, int user_id)
{
size_t n_bytes = sizeof(struct Cxx_ABI_Header) + sizeof(struct object_one);
void * pheap = malloc(n_bytes);
struct * pCxx_ABI_Header pcxx = pheap;
struct * pObject pobj = (void *)((char *)pheap
+ sizeof(struct Cxx_ABI_Header));
if (!pheap) ...
pcxx->inheritance_backward_offset = 0;
pcxx->rtti = /* You tell me? */
pcxx->vtable = (userid < 0 ) ? CRUD_vtable_guest
: CRUD_vtable_authenticated_user;
pobj->file_name = strdup(filespec);
pobj->file_descriptor = 0;
return pobj;
}
Voila - polymorphism via ?:
Anyway, enjoy the language experimentation and good luck improving on C++. By basing your efforts on C, you would be off to a solid start. ;)