Is it possible to subclass a C struct in C++ and use pointers to the struct in C code?

后端 未结 10 1072
滥情空心
滥情空心 2021-02-07 00:10

Is there a side effect in doing this:

C code:

struct foo {
      int k;
};

int ret_foo(const struct foo* f){ 
    return f.k; 
}

C++ c

10条回答
  •  情书的邮戳
    2021-02-07 00:45

    It will work, and portably BUT you cannot use any virtual functions (which includes destructors).

    I would recommend that instead of doing this you have Bar contain a Foo.

    class Bar
    {
    private:
       Foo  mFoo;
    };
    

提交回复
热议问题