473,406 Members | 2,769 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,406 software developers and data experts.

class and method

How do I simulate(to a minimal extent) the
notion of classes and methods in C ?
Nov 13 '05 #1
5 9562
On Sun, 23 Nov 2003 22:30:54 -0800, Aniruddha wrote:
How do I simulate(to a minimal extent) the
notion of classes and methods in C ?


struct my_class {
int blah;
};
my_class *new_myclass() {
struct my_class *this;
this = malloc(sizeof *this);
/* error handling here */
}

void delete_myclass(struct my_class *this) {
free(this);
}

void my_method(struct my_class *this) {
do_something_to(this);
}

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Nov 13 '05 #2
Nils Petter Vaskinn wrote:
On Sun, 23 Nov 2003 22:30:54 -0800, Aniruddha wrote:
How do I simulate(to a minimal extent) the
notion of classes and methods in C ?
struct my_class {
int blah;
};

my_class *new_myclass() {


struct my_class *new_myclass() {
struct my_class *this;
this = malloc(sizeof *this);
/* error handling here */
return this;
}

void delete_myclass(struct my_class *this) {
free(this);
}

void my_method(struct my_class *this) {
do_something_to(this);
}


You may want to add function pointers to struct my_class to
emulate the C++ notion even better.

Jirka
Nov 13 '05 #3
On Mon, 24 Nov 2003 13:56:57 +0100, Jirka Klaue wrote:
Nils Petter Vaskinn wrote:
On Sun, 23 Nov 2003 22:30:54 -0800, Aniruddha wrote:
How do I simulate(to a minimal extent) the
notion of classes and methods in C ?
my_class *new_myclass() {
struct my_class *new_myclass() {


doh.
struct my_class *this;
this = malloc(sizeof *this);
/* error handling here */
return this;


doh!

I need to proofread my posts. But I think I got the general idea across.
You may want to add function pointers to struct my_class to
emulate the C++ notion even better.


Depending on the definition of "minimal extent". But for every step
towards "real" classes it becomes more complex and the OP should consider
actually using C++ instead.
--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Nov 13 '05 #4
Nils Petter Vaskinn wrote:
On Mon, 24 Nov 2003 13:56:57 +0100, Jirka Klaue wrote:

....
You may want to add function pointers to struct my_class to
emulate the C++ notion even better.


Depending on the definition of "minimal extent". But for every step
towards "real" classes it becomes more complex and the OP should consider
actually using C++ instead.


:-) Very true.

Jirka
Nov 13 '05 #5
Aniruddha wrote:
How do I simulate(to a minimal extent) the
notion of classes and methods in C ?


Take a look at
The ANSI C Numerical Class Library
http://www.netwood.net/~edwin/svmtl/

Also, run-time polymorphism has been discussed at length
in the comp.lang.c newsgroup. See Google Groups

http://groups.google.com/

and search for

Tisdale Shape group:comp.lang.c.*

Both Re: "class" in C and Re: C version of C++'s virtual functions
contain an ANSI C implementation of Bjarne Stroustrups Shape class.

Nov 13 '05 #6

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

Similar topics

4
by: Edvard Majakari | last post by:
Hi, I just found py.test and converted a large unit test module to py.test format (which is actually almost-no-format-at-all, but I won't get there now). Having 348 test cases in the module and...
10
by: lkrubner | last post by:
I killed last night and a good chunk of today trying to figure out this one particular attempt to get a class and initialize it. My code is using a class method called getObject to include() a file...
4
by: Amy Matlock | last post by:
Hi all: How does the hardware work if you invoke a BASE::METHOD() on a DERIVED class member? Do you still hit the v-table dynamically at run time? Suppose you have a derived class method, but...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
4
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
1
by: question | last post by:
I want to know incase there is any performance difference or overhead in calling a base class method and a derived class method. Basically I am talking about simple method that is not overridden...
2
by: Sylvain Ferriol | last post by:
hello can you explain why python does not see difference between instance method and class method, having the same name example .... def f(self): .... print('instance method')...
0
by: alwayssmiling | last post by:
hi, I created an abstract class. In that a method which returns same class object, like predifned class, system.drawing.image a = system.drawing.image.fromfile(filename) this...
13
by: Hussein B | last post by:
Hi, I'm familiar with static method concept, but what is the class method? how it does differ from static method? when to use it? -- class M: def method(cls, x): pass method =...
0
by: jbvbprgrmr | last post by:
Is it possible to call a .NET shared class method from a VB6 application? I created a small test project consisting of a .NET class library containing the following class: Public Class MyTest...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
agi2029
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,...
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.