Hi all,
I want to do a similar thing as class inheritance in C++, but with structures in C.
I have a structure A, and I want to define a second structure B (in different .h/.c files), which has the same variables as A plus some additional ones. I want to use the existing functions that manipulate and create A also for B and, using some new functions, add the bits about the additional variables. If possible, it will not be necessary to change B and the new functions if A is modified.
Maybe some clever redefinitions using the prepcocessor can be used?
Thanks for your help,
Martin
3 18574 JosAH 11,448
Recognized Expert MVP
Hi all,
I want to do a similar thing as class inheritance in C++, but with structures in C.
I have a structure A, and I want to define a second structure B (in different .h/.c files), which has the same variables as A plus some additional ones. I want to use the existing functions that manipulate and create A also for B and, using some new functions, add the bits about the additional variables. If possible, it will not be necessary to change B and the new functions if A is modified.
Maybe some clever redefinitions using the prepcocessor can be used?
Thanks for your help,
Martin
In C a pointer to a struct can safely be casted to a pointer to the first element
of that struct and vice versa. The ANSI/ISO standard explicitly defined so. That
opens doors to do simple single inheritance emulation: -
struct Base {
-
...
-
}
-
struct Derived {
-
struct Base parent;
-
...
-
}
If you want to do virtual functions as well you have to make the first member
of your structs an array of pointers to functions. The tricky part will be the
initialization of such an array and the cpp fiddling necessary to reach such a
pointer to a function.
kind regards,
Jos
One of the secrets of C++ is that there are no classes. C++ is derived form C and in C the only syntax to define your own types with members is a struct. Classes have been implemented in C++ as structs.
That means:
1) you use inheritance with structs
2) you can have constructors/destructors with structs
3) virtual functions
4) etc... etc...
The only difference between a class and a struct is that the default access specifier for a class is private whereas the default for a struct is public. Therefore, if you specify public/private/protected for each one of your struct members, there is no difference between that and a class.
JosAH 11,448
Recognized Expert MVP
One of the secrets of C++ is that there are no classes. C++ is derived form C and in C the only syntax to define your own types with members is a struct. Classes have been implemented in C++ as structs.
True, all true but the OP wanted to implement (part of) the shebang in C only,
no C++.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Susan Baker |
last post by:
Hi,
I want to store data in a 3-tuple {a,b,c}.
Element a is an enumeration, and element c is an enumeration which is
specific (i.e. determined) by a.
An example will help clarify further. I give two examples, one of a
valid instance, and the other, an invalid instance.
typedef enum { bovine, gamebird, seafood } general_meat ;
|
by: Neils Christoffersen |
last post by:
Hey all,
I wrote on Friday asking for some help moving a common subclass field up
to the base class (original post and followup included below). This
entails storing whole numbers inside float data types. What I'm stuck on
is how to make sure that the value coming in is indeed a whole number,
without converting it to a string and doing...
|
by: Laharl |
last post by:
This is what I am trying to do:
public abstract class A : ISerializable
{
public A(SerializationInfo info, StreamingContext context){}
public void GetObjectData(SerializationInfo info, StreamingContext
context) {}
}
public abstract class B : A, ISerializable
|
by: Helge Jensen |
last post by:
I've got some data that has Set structure, that is membership, insert
and delete is fast (O(1), hashing). I can't find a System.Collections
interface that matches the operations naturally offered by Sets.
- ICollection cannot decide containment
- IList promises indexability by the natural numbers, which is not
achievable (since i hash...
|
by: J.M. |
last post by:
I have a question concerning inheritance: can an abstract (parent) class
have an abstract object? I would like to make a concrete child inherit from
this class by inheriting from this object. Let me try to make this clear by
example: The following should be "abstract".
"class motor" is abstract because it has a virtual member
"virtual int...
| |
by: Fao |
last post by:
Hello, I am having some problems with inheritance. The compiler does
not not return any error messages, but when I execute the program, it
only allows me to enter the number, but nothing else happend. I think
the problem may be in my input function or in the main function.
If anyone out there can help me it woul be greatly appreciated.
...
|
by: Joe HM |
last post by:
Hello -
I have a *.dll that was compiled from Ada. The following declaration
and Structure is working ...
Declare Sub XXX Lib "X.dll" (ByRef XState As XStructure)
<StructLayout(LayoutKind.Explicit)_
Structure XStructure
<FieldOffset(0)Dim A As Single
|
by: Arthur Dent |
last post by:
Hello all...
I have a method which returns a KeyValuePair(Of Long, String). I would like
to make an alias for that, so instead of typing KeyValuePair(Of Long,
String) everywhere I could just type my alias instead.
First I thought inheritance, ... Structure MyPair Inherits KeyValuePair(Of
Long, String), but you can't inherit structures....
|
by: Pallav singh |
last post by:
Hi
i find Following statement while reading Design pattern in C++
( author - Erich Gamma)
"Inheritance break Encapsulation " because inheritance exposes to
subclass the detail of its parent's Implementation
Is it correct ???
as we only expose interface ( function prototype) to derived class But
not how it being implemented
|
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...
|
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. ...
| |
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |