Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > struct and function-pointer casting

Reply
Thread Tools

struct and function-pointer casting

 
 
Maurice
Guest
Posts: n/a
 
      04-29-2005
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.
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      04-29-2005


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).

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Maurice
Guest
Posts: n/a
 
      04-30-2005
Eric Sosman <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Up casting and down casting Sosuke C++ 2 12-20-2009 03:24 PM
Problem with depracated casting method (down casting) Wally Barnes C++ 3 11-20-2008 05:33 AM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments