473,654 Members | 3,040 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

struct and function-pointer casting

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)(str uct Base*);
};

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

void derived_func(st ruct Derived* d) {}

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

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

Thank you.
Nov 14 '05 #1
2 2041


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)(str uct Base*);
};

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

void derived_func(st ruct 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(st ruct 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).

--
Er*********@sun .com

Nov 14 '05 #2
Eric Sosman <er*********@su n.com> 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(st ruct 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.
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2481
by: _mario.lat | last post by:
I have a struct like that (for a graph): struct edge { int nodea; int nodeb; int weight; } I have an array of this struct: struct e edge;
20
12403
by: Elliot Marks | last post by:
If a struct or its members are passed to a function, must it be declared globally? #include <stdio.h> struct mystruct{ int a; int b; }; int structfunc(struct mystruct foo);
2
2461
by: Arne Styve | last post by:
Hi, I have an API written in C that came with some graphics cards we are going to use in a project. I need to write a small application where this API is to be used, and I decided to try out C#. The API is imported by using the ; unsigned short DeviceType; unsigned long Reserved0; unsigned long Reserved1;
12
3577
by: Sadeq | last post by:
Is there a way to read a struct field by field? I want to write a rather general function, like: public string EncodeStruct (struct strct) { .... } which accepts a general struct type, reads it field by field, encodes
20
2105
by: ma0001yu | last post by:
Hi, all. I feel confuse about below struct definition: typedef struct TAG { comments.... }; What my confusion is: is typedef extra??why we not just use
5
29346
by: jaso | last post by:
Hi, If have a structure of a database record like this: struct record { char id; char title; ... }; Is there some way to find out how many member variables there is in the struct and then iterate through them?
2
2280
by: James Brown | last post by:
All, Could anyone explain why the following is an error: void foo( struct FOO { int x; } f1 ) { } int main()
4
5710
by: acap | last post by:
my problem is converting C to java, pls help..... this the code.... #include <stdio.h> #include <string.h> #include <ctype.h> #define FIRST_NAME_LEN 31 #define SECOND_NAME_LEN 51
2
7462
by: terabits | last post by:
hi i have a global structure , and its reference is created in main and passed to the function where i want to update the values of the structure whenever the function is called. here i want to assing in a simple way like the way we initialize a structure is this possible.. ?? or any otheway something like this please let me know regards
160
5826
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
8375
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8482
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8593
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.