473,513 Members | 2,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using C as a OO langauge

41 New Member
Hi
I've been using C++ quite a lot now, but i have recently moved over to C, and because of my experience with C++ i tend to use C in the same way as I used C++.

e.g:
Expand|Select|Wrap|Line Numbers
  1. typedef struct SMyStruct
  2. {
  3.    int A,B,C;
  4. } MyStruct;
  5.  
  6. int myStruct_set_values(MyStruct* s, int a, int b, int c)
  7. {
  8.    s->A = a;
  9.    s->B = b;
  10.    s->C = c;
  11. }
  12.  
I feel that I'm using C the wrong way, as I'm really creating objects and member functions.
Is the above code following conventions?
If not, how would you do it?
Mar 6 '08 #1
2 976
gpraghuram
1,275 Recognized Expert Top Contributor
Hi
I've been using C++ quite a lot now, but i have recently moved over to C, and because of my experience with C++ i tend to use C in the same way as I used C++.

e.g:
Expand|Select|Wrap|Line Numbers
  1. typedef struct SMyStruct
  2. {
  3.    int A,B,C;
  4. } MyStruct;
  5.  
  6. int myStruct_set_values(MyStruct* s, int a, int b, int c)
  7. {
  8.    s->A = a;
  9.    s->B = b;
  10.    s->C = c;
  11. }
  12.  
I feel that I'm using C the wrong way, as I'm really creating objects and member functions.
Is the above code following conventions?
If not, how would you do it?

I dont feel what u are doing is wrong.
Its better to have functions in C which does a specific job.
The only difference is that it may not be part of the structure directly.

Raghuram
Mar 7 '08 #2
Banfa
9,065 Recognized Expert Moderator Expert
This is slightly unusual and I have only really seen this done like this in C where the code is hiding the type from the calling code

e.g:
Expand|Select|Wrap|Line Numbers
  1. typedef struct SMyStruct
  2. {
  3.    int A,B,C;
  4. } MyStruct;
  5.  
  6. int myStruct_set_values(void *vs, int a, int b, int c)
  7. {
  8.     MyStruct *s = vs;
  9.  
  10.    s->A = a;
  11.    s->B = b;
  12.    s->C = c;
  13. }
  14.  
Somewhere they would be a function that creates a MyStruct and returns a pointer to it cast to void *

This is the basic set-up for an Abstracted Data Type (ADT). In a type like this the calling code has no direct access to the data which is enforced by keeping the data structures private to the ADT internal code. The calling code is returned a unique identifier to it's data (this could be a void * as above or any identifying int that allows the ADT to correctly locate the data in question). The calling code effectively requests data and then calls function to manipulate the data all in the ADT but never has direct access to the data giving the designer of the ADT much better control of it (unless some numpty circumvents the whole thing by hacking direct access to the ADT's data).
Mar 7 '08 #3

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

Similar topics

5
12966
by: Jerry | last post by:
Hi, I am writing a Java Chatroom application that will implement encryption of messages using the RSA algorithm using the BigInteger class. It uses socket connections to exchange messages. I...
6
326
by: Punya Narra | last post by:
Hi All, I have a choice of using VB and .Net in developing a standalone load distribution application. I need to provide a case study on what are the major advantages of using .Net with respect...
2
1643
by: ahmed ahmed | last post by:
VB.NET hi i'm asking about How To Change System Keyboard langauge from the VB.NET or Keyboard langauge from langauge to another without the API function. and thanks about helping me. ***...
1
1350
by: Dennis Gearon | last post by:
I'd like to get people's feelings about the topic. At one extreme is to use table locking and external language queries to even do referential integerity - a la Old (present?) MySQL/PHP. A more...
6
1403
by: Rulla | last post by:
Hi , It is my ifrst time trying to use any script langauge , I have to learn it by today and tomorrow because I am doing my dissreattion and I need it to my project so I will appreciate any help as...
2
12291
by: =?Utf-8?B?U3VuaWwgUGFuZGl0YQ==?= | last post by:
Hi All, I have created a VB.Net application that used classic ADO to access the oracle database. I have mentained a persistant connection in it. When i start the application and it works fine....
2
985
yoda
by: yoda | last post by:
i was wondering what langauge should i use to make a game for the computer. what do you suggest Like C++ or VB or VBA thanks a lot Yoda
4
4280
by: dotnetbeginner | last post by:
ASP .net -- VB .net -- IIS 5.1 --.NET 2.0 I want to perform excel automation on the server side and I wrote an asp .net page with the langauge vb .net. The goal of this program was to open an...
2
3600
by: ARAVINDRNR | last post by:
How to change an unsigned char array to bit map file in 'C' langauge
0
7265
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
7171
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...
0
7388
Oralloy
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,...
0
7545
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
5692
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,...
1
5095
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...
0
3240
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...
0
3228
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1605
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 ...

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.