我有一个伪或特殊属性的对象,可以用三种不同的方式命名(注意:我不控制生成对象的代码)
属性中的值(取决于哪一个)是完全相同的,我需要得到它以进行进一步处理,因此根据数据源,我可以有类似的东西:
>>> obj.a
'value'
>>> obj.b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Obj instance has no attribute 'b'
>>> obj.c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Obj instance has no attribute 'c'
Run Code Online (Sandbox Code Playgroud)
要么
>>> obj.a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Obj instance has no attribute 'a'
>>> obj.b
'value'
>>> obj.c
Traceback (most recent call last): …Run Code Online (Sandbox Code Playgroud) 我今天一直在玩C,而且我从来没有机会玩过它,那就是使用一个指向功能的结构......好吧一切顺利,直到我开始得到一些奇怪的bug,当我清理时整个事情(顺便说一下:我在x86_64 arch中编译,在Mac中)看起来和看起来我发现这是结构中的内存对齐node_vtable.
在i386中,它工作得很好......没有任何问题.但是,正如我在x86_64中所说,它不起作用.
/* NOT WORKING */
struct node_vtable {
void (*add_node) (linked_list *, node *);
node * (*create_node) (linked_list *, float, int, int );
void (*print) (linked_list *llist);
};
/* WORKING */
struct node_vtable {
void (*print) (linked_list *llist);
void (*add_node) (linked_list *, node *);
node * (*create_node) (linked_list *, float, int, int );
};
Run Code Online (Sandbox Code Playgroud)
现在,我修复了将指针移动node * (*create_node) (linked_list *, float, int, int );到结构的末尾,因为它的大小为24字节,这是该结构中最大的.但是,我真的认为有一个更优雅的解决方案,而且我正在寻找一个明确的解释.因此,如果有人可以给出一个提示或一些解释,这将是伟大的;)....因为我的大脑现在真的卡住了:)
整个代码:
#include <stdio.h>
#include <stdlib.h>
struct node {
int id;
int …Run Code Online (Sandbox Code Playgroud)