LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-06-2009, 01:39 PM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
"Object oriented programming" 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?
 
Old 12-06-2009, 01:55 PM   #2
ozanbaba
Member
 
Registered: May 2003
Location: İzmir
Distribution: Slackware64 15.0 Multilib
Posts: 778

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by MTK358 View Post
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?
http://bytes.com/topic/c/answers/632...-inheritance-c
 
Old 12-06-2009, 02:10 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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.
 
Old 12-06-2009, 03:12 PM   #4
ozanbaba
Member
 
Registered: May 2003
Location: İzmir
Distribution: Slackware64 15.0 Multilib
Posts: 778

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by MTK358 View Post
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.
 
Old 12-06-2009, 03:13 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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.

Last edited by MTK358; 12-06-2009 at 03:16 PM.
 
Old 12-06-2009, 03:48 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
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++).
 
Old 12-06-2009, 04:23 PM   #7
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: "North Shore" Louisiana USA
Distribution: Mint-20.1 with Cinnamon
Posts: 1,772
Blog Entries: 3

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by MTK358 View Post
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.

~~~ 0;-Dan
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Object Oriented Programming in the Web entz Programming 8 08-04-2009 05:10 AM
LXer: Object Oriented Programming in Python LXer Syndicated Linux News 0 01-07-2009 06:20 AM
LXer: Article: Why you want Object Oriented Programming in PHP LXer Syndicated Linux News 0 03-30-2007 07:16 PM
perl object Oriented Programming Question eastsuse Programming 1 08-20-2004 12:29 PM
Problem with "Object Oriented Concepts" Mohsen Programming 6 03-24-2004 12:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration