struct and function-pointer casting

Discussion in 'C Programming' started by Maurice, Apr 29, 2005.

  1. Maurice

    Maurice Guest

    Hi,

    Is it legal to cast from void(*)(A*) to void(*)(B*)?
    Is it legal to cast from struct Derived{Base b; ...} to struct Base?

    I'm trying to get some inheritance and polymorphism in C
    and I end up with the following code:

    struct Base {
    void(*func)(struct Base*);
    };

    struct Derived {
    struct Base base;
    /* ... */
    };

    void derived_func(struct Derived* d) {}

    int main() {
    struct Derived d = {{derived_func} /*...*/ };

    struct Base* b = &d;
    b->func(b);
    }

    Thank you.
     
    Maurice, Apr 29, 2005
    #1
    1. Advertising

  2. Maurice

    Eric Sosman Guest

    Maurice wrote:
    > Hi,
    >
    > Is it legal to cast from void(*)(A*) to void(*)(B*)?
    > Is it legal to cast from struct Derived{Base b; ...} to struct Base?
    >
    > I'm trying to get some inheritance and polymorphism in C
    > and I end up with the following code:
    >
    > struct Base {
    > void(*func)(struct Base*);
    > };
    >
    > struct Derived {
    > struct Base base;
    > /* ... */
    > };
    >
    > void derived_func(struct Derived* d) {}
    >
    > int main() {
    > struct Derived d = {{derived_func} /*...*/ };
    >
    > struct Base* b = &d;
    > b->func(b);
    > }


    No; the type of `derived_func' does not match the
    type of the `func' element in `struct Base', so the
    compiler should object to the initialization. One
    simple fix is

    void derived_func(struct Base *b) {
    struct Derived *d = (struct Derived*)b;
    ...
    }

    The conversions of the struct pointers are all right:
    any struct pointer can be converted to and from a pointer
    to the struct's first element (unless that element is a
    bit-field; you can't point to a bit-field).

    --
     
    Eric Sosman, Apr 29, 2005
    #2
    1. Advertising

  3. Maurice

    Maurice Guest

    Eric Sosman <> wrote:
    > No; the type of `derived_func' does not match the
    > type of the `func' element in `struct Base', so the
    > compiler should object to the initialization. One
    > simple fix is
    >
    > void derived_func(struct Base *b) {
    > struct Derived *d = (struct Derived*)b;
    > ...
    > }


    Ok, not as neat as it would be in my code but I think I can
    live with it.

    > The conversions of the struct pointers are all right:
    > any struct pointer can be converted to and from a pointer
    > to the struct's first element (unless that element is a
    > bit-field; you can't point to a bit-field).


    That is good.

    Thanks for your answer.
     
    Maurice, Apr 30, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ken
    Replies:
    3
    Views:
    2,523
    Rolf Magnus
    Nov 8, 2003
  2. Ole
    Replies:
    4
    Views:
    606
    Michael Wojcik
    Oct 26, 2004
  3. beetle
    Replies:
    2
    Views:
    926
    beetle
    Jan 25, 2005
  4. Zero
    Replies:
    16
    Views:
    667
    Barry Schwarz
    Nov 19, 2005
  5. aleksa

    Struct pointer vs. struct array pointer

    aleksa, Feb 20, 2013, in forum: C Programming
    Replies:
    16
    Views:
    485
    Shao Miller
    Feb 20, 2013
Loading...

Share This Page