V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
mdzz
V2EX  ›  C

有没有办法告诉编译器两个结构体是等价的

  •  
  •   mdzz · 2017-09-11 14:16:59 +08:00 · 2275 次点击
    这是一个创建于 2412 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如有 struct A 和 struct B,在需要使用 struct A 的地方也可使用 struct B,而且不会出现错误与警告,例如

    int func(struct A *a);
    
    struct B b;
    func(&b);
    

    注意,以下答案不能达到目的:

    typedef struct A B;
    
    union U {
    	struct A a;
    	struct B b;
    };
    
    15 条回复    2017-09-12 13:18:19 +08:00
    fyyz
        1
    fyyz  
       2017-09-11 14:20:23 +08:00 via Android
    继承
    XiaoxiaoPu
        2
    XiaoxiaoPu  
       2017-09-11 14:29:34 +08:00 via iPhone
    指针强制类型转换

    func((struct A *)&b);
    momocraft
        3
    momocraft  
       2017-09-11 14:33:48 +08:00
    #define B A
    Panic
        4
    Panic  
       2017-09-11 14:34:43 +08:00
    不能用 union ?那没得救了,重写吧
    northisland
        5
    northisland  
       2017-09-11 14:59:11 +08:00
    C++版

    #include <isotream>

    struct A {
    A(int d): digit(d) {}
    int digit;
    };
    struct B: public Base {
    B(int d): Base(d) {}
    };

    int function(struct Base& p_base) {
    std::cout << p_base->digit << std::endl;
    }

    int main() {
    A a(89);
    B b(64);

    func(&a);
    func(&b);
    return 0;
    }
    northisland
        6
    northisland  
       2017-09-11 15:01:14 +08:00
    #include <isotream>

    struct A {
    A(int d): digit(d) {}
    int digit;
    };
    struct B: public A{
    B(int d): A(d) {}
    };

    int function(struct A& p_base) {
    std::cout << p_base->digit << std::endl;
    }

    int main() {
    struct A a(89);
    struct B b(64);

    func(&a);
    func(&b);
    return 0;
    }
    liuminghao233
        7
    liuminghao233  
       2017-09-11 15:14:12 +08:00 via iPhone
    强转吧
    acros
        8
    acros  
       2017-09-11 15:19:21 +08:00
    reinterpret_cast
    回想了以下,以前工程里面我是没遇到什么机会用这个。
    kofj
        9
    kofj  
       2017-09-11 15:55:24 +08:00
    Golang interface 乱入
    fy
        10
    fy  
       2017-09-11 16:03:34 +08:00
    要么 Union,要么类型强转,别跟标准过不去
    gnaggnoyil
        11
    gnaggnoyil  
       2017-09-11 20:58:10 +08:00
    LZ 的头像完美的展示了 C 语言标准面对 LZ 这个需求的时候的反应.
    lrxiao
        12
    lrxiao  
       2017-09-12 04:20:36 +08:00
    andrewhxism
        13
    andrewhxism  
       2017-09-12 10:45:09 +08:00
    @momocraft #3 正解!
    andrewhxism
        14
    andrewhxism  
       2017-09-12 10:46:12 +08:00
    @momocraft #3 还有一个办法是 Ctrl + F
    momocraft
        15
    momocraft  
       2017-09-12 13:18:19 +08:00
    正经的回答:

    如果这两个 struct 真的只有名字不同,应该可以用 typedef 或#define。

    既然需要当问题拿出来,我猜这两个 struct 不同内容。此时编译器需要的不仅是一个等价的"保证",还需要知道一个 struct 的 member 怎样对应到另一个 struct。
    编译器是不会去猜 member 的对应关系的,人都未必猜得出。你可以自己写一些 getter/setter 函数来实现 C 不提供的重载。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2969 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 11:12 · PVG 19:12 · LAX 04:12 · JFK 07:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.