ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I know that you can do objects in C by having a struct with the data and functions that take the struct as their first parameter. I was wondering if it is possible somehow to do struct inheritance in C?
I know that you can do objects in C by having a struct with the data and functions that take the struct as their first parameter. I was wondering if it is possible somehow to do struct inheritance in C?
I thought about the derived struct having a pointer to it's parent, but that won't work because for example we have the struct Shape and a function that takes Shape as it's parameter. Now what if we create a derived struct for Shape called Rectangle. How can you pas the Rectangle to the Shape function?
You can give the Shape function the Rectangle's pointer, but what if you do not know the exact hierarchy? The code will soon be a big, hard-to-understand mess.
I thought about the derived struct having a pointer to it's parent, but that won't work because for example we have the struct Shape and a function that takes Shape as it's parameter. Now what if we create a derived struct for Shape called Rectangle. How can you pas the Rectangle to the Shape function?
You can give the Shape function the Rectangle's pointer, but what if you do not know the exact hierarchy? The code will soon be a big, hard-to-understand mess.
exactly, with pointer, you get one important problem you just said. poorly
documented mess. i don't see an other way to do.
you can do by hand hertigace by pointing a function on an other struct's function. but that sounds silly thing to do.
Reading the Wikipedia article on "C++ classes" said that most C++ compilers implement inheritance by concatenating the derived class's members to the base class's, so the relative index in memory is the same wether you get the base or derived class.
Here's a diagram:
Code:
Class: Variable: Index:
A foo 0
B bar 1
So if you pass an instance of struct B to a function that takes a struct A, you still get the right variable because the index is the same.
But I don't know how to make an elegant implementation of this in C, especially when the instance variables are of different sizes.
EDIT: And I read in another article that it is a bad idea for the struct to have function pointers because you get huge amounts of redundant data if you have a lot if instances.
So if you pass an instance of struct B to a function that takes a struct A, you still get the right variable because the index is the same.
But I don't know how to make an elegant implementation of this in C, especially when the instance variables are of different sizes.
In C++ you can pass a pointer (or reference) to class B to a function that takes a pointer to class A. That can be replicated in C, except that you will have to manually cast or the compiler will complain about type mismatches.
Quote:
EDIT: And I read in another article that it is a bad idea for the struct to have function pointers because you get huge amounts of redundant data if you have a lot if instances.
You just need one function table for each class, and every instance needs only the one pointer to this table (this is called the vtable in C++).
I know that you can do objects in C by having a struct with the data and functions that take the struct as their first parameter. I was wondering if it is possible somehow to do struct inheritance in C?
Review the plain-C function family for fopen( ) fclose( ) fread( ) and fwrite( ). All of these hang off a common struct{ }.
In your stated case with struct A{ } and then struct B{ struct A; ...}. I believe this is an example of a "uses" relationship -- specifically,
B uses A.
I have included a void * as a struct member and then loaded the items with a pointer to various A1, ..., An structs. A second member would indicate which of the various A-items was in place. Passing B around this way, when I needed the contents of the A-item, I would cast the void * to be the explicit A-item type that I needed.
Written plain-C since the early seventies.
Used C++ as "a better C compiler" since the late nineties.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.