CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Member functions inside structures?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Neil_J



Joined: 11 Jun 2006
Posts: 4

View user's profile Send private message

Member functions inside structures?
PostPosted: Wed Sep 13, 2006 10:56 am     Reply with quote

I've used C for a while, and am currently learning C++ for work. In one of my C++ books, they mention using member functions inside a structure, similar to using them inside classes (although they are rarely used)

Code:
struct data
{
     int status;
     int data;

     void start()
     {
          status = 1;
     }
};


Is this possible in CCS, and does anyone use it? If not, are there other ways to keep variables and functions object-oriented in C? (style, etc).
kam



Joined: 15 Jul 2005
Posts: 59

View user's profile Send private message

PostPosted: Wed Sep 13, 2006 2:52 pm     Reply with quote

Neil,

One of the biggest mistakes or gotch'yas in OO programming is over encapsalation. I see this over and over again with people starting OO programming.

CCS is not an ANSI C compiler, it can be considered --C Razz Razz since it does not have some basic C/C++ support (pointers etc)

Having methods within a Class or Struct is nice as it allows you to package "stuff" (variable, methods, structs etc) within a single shell. So what? you can do that just as easily with a well thought out header file. Where classes come really into play is when you start doing things like abstract methods or even polymorphic, thats where it really shines.

pointer->method() can point to a different method() based on context (assuming you have many method() functions). It is truely geat stuff.

NOW, back to your question...

A very simple and elegant way to handle OO type of support is to encapsalate data (not methods since C does not allow that), and then pass that data to a generic method, now you can manage different data to a single method. If CCS supported pointers, you could put a pointer to a method within the struct and run it from there...yes?...

For example, say you want to create a draw program that can draw different shapes, circle, box, etc.

You could;

#1. Have a different method per draw type, and call the correct one (C/C++)
#2. Have a base class with an abstract "draw" method, and the new class would provide the "draw" method, then calling from pointer->draw() would call the correct method (C++)
#3. Kind of like #1 & #2, have a method that takes a struct arg, and within that method it examines the sent data and passes it to the correct draw method.

Take my advice from very painful experiences, over encapsilation will kill you! Shocked You end up having a class that contains everything and the kitchen sink because it's so tightly packed in that you cannot strip it down and you have to carry all this extra junk with it.

A Class or Struct should contain a SINGLE concept; all the methods within that class support this SINGLE concept. A concept is draw a box; a circle; add 2 ints; push this data down some pipe.

Class are a very powerful C++ feature.

I think I've been rambling on here, so I'll stop. I don't think I actually answered anything! Very Happy Very Happy Very Happy

~Kam (^8*
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group