473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

structure "inheritance" in c

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
Apr 15 '07 #1
3 18564
JosAH
11,448 Expert 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. struct Base {
  2.    ...
  3. }
  4. struct Derived {
  5.    struct Base parent;
  6.    ...
  7. }
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
Apr 15 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 15 '07 #3
JosAH
11,448 Expert 8TB
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
Apr 15 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
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...
3
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...
0
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,...
21
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...
4
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...
13
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...
0
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) ...
1
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...
1
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.