这取决于您要编译到的硬件平台,但布局通常在不同的实现中非常相似。毕竟最早的C++是CFRONT,它把C++编译成C...
平台相关问题和内存布局将在“平台 C++ ABI”中描述,其中 ABI 代表“应用程序二进制接口”。
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 }};
这是一个可能的构造函数,它实际上产生了两种不同的“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;
}
瞧——通过?:实现多态性
无论如何,享受语言实验,祝你在 C++ 上取得进步。通过将您的努力建立在 C 上,您将有一个良好的开端。 ;)