PDA

View Full Version : classes in c?



CT_Bolt
November 19th, 2006, 20:43
I want to make a class or something like a class in c... know C++ pretty well but, I just started c a little bit ago anyway heres my problem:

I know this is how to create a class in c++

class foo
{
public:
foo(int = 0, int = 0);
int a, b;
};

foo::foo(int c, int d)
{
a = c;
b = d;
}

could someone please show me how to convert the c++ class into c

quzar
November 19th, 2006, 22:09
You can't make a class in C. What you can do, is something like this:


struct foo_t{
int a = 0;
int b = 0;
} foo;
//The above makes an 'object' of type foo_t, called foo. you can then do this:

void populate_foo(int c, int d){
foo.a = c;
foo.b = d;
}

Basically if you are taking a whole class structure and converting it to C you can usually create a struct with all the data the class usually held. You can also insert objects of type returntype (* functionname) (type name, type name ...); which would be a pointer to a function. With something like that, what you could do is have something like....

int add_foo_t(foo_t thefoo){
return thefoo.a+thefoo.b;
}
then make the struct be:

struct foo_t{
int a = 0;
int b = 0;
int (*add)(foo_t);
} foo;

and the init be:

void init_foo(int c, int d){
foo.a = c;
foo.b = d;
foo.add = add_foo_t;
}

now later on you can call (foo.add)(foo); to get a+b.

Hope these examples weren't too much, I realize I kind of went overboard compared to what you asked for =P.

CT_Bolt
November 19th, 2006, 23:04
Thank you so much!!! This is exactly what I was looking for... your so kind. I'm glad you went overboard also. It's not too much it's perfect... I may have more trouble with it in the future but, this is just what i need for now... THANK YOU AGIAN! :thumbup:

quzar
November 19th, 2006, 23:10
Another small point. If you typedef a struct, then you can create new ones later.

CT_Bolt
November 19th, 2006, 23:14
You mean like this?

typedef struct foo_t{
int a = 0;
int b = 0;
} foo;

or no?

if not could you please show me an example?

yaustar
November 19th, 2006, 23:47
You can create new instances of the struct (using quzar's example) by doing the following:

struct foo_t goo;
struct foo_t dfhdsflhf;
struct foo_t etc;

They are all instances of foo_t.

By using typedeft like so:

typedef struct foo_t{
int a = 0;
int b = 0;
} foo;

You create new instances by:
foo goo;
foo dfhdsflhf;
foo etc;

CT_Bolt
November 20th, 2006, 18:31
Could you please tell me what is wrong with this?

typedef struct foo_t
{
int a = 0;
int b = 0;
int (foo_t);
} foo;

void init_foo(int c, int d)
{
foo.a = c;
foo.b = d;
}


psp-gcc -I. -IC:/devkitpsp/psp/sdk/include -O2 -G0 -Wall -c -o main.o main.c
main.c:21: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token
main.c: In function 'init_foo':
main.c:28: error: expected identifier or '(' before '.' token
main.c:29: error: expected identifier or '(' before '.' token
make: *** [main.o] Error 1
I know its probably simple but I just don't get it... :confused: :confused: :confused:

quzar
November 21st, 2006, 08:06
take away the typedef. also the function pointer has to have a name, so int (*add)(foo_t); instead of int (foo_t);

that should fix it (don't have a dev environment around.

CT_Bolt
November 21st, 2006, 17:03
First of all THANK YOU ALL FOR HELPING ME.

What I want to do is make my own data type containing other data using '.' tokens. I get these errors when I do this though.

struct Point_t pn;

typedef struct Point_t{
int X, Y;
} Point;

void init_Point(int a, int b){
Point.X = a; // error: expected identifier or '(' before '.' token
Point.Y = b; // error: expected identifier or '(' before '.' token
}

If you know what these errors mean please let me know.

I need to be able to initialize the Point when I declare it though. How do I do that?:confused:

quzar
November 21st, 2006, 17:24
typedef struct Point_t{
int X, Y;
} Point;

struct Point pn;

void init_Point(int a, int b){
pn.X = a;
pn.Y = b;
}

CT_Bolt
November 21st, 2006, 18:33
I sorta figured something out...

typedef struct clsPoint{
int X, Y;
} Point;

Point createPoint(int a, int b){
Point p;
p.X = a;
p.Y = b;
return (p);
}

Point pn = createPoint(10,15);

I need to know 2 other things now though...

1. Is there a more efficient way to do what I did above?
2. How to make a function that I can use like this:

pn.Move(2, 5); // Moves the point over 2 and down 5.

Could you help me one more time please. Sorry I ask so many questions.

yaustar
November 21st, 2006, 21:17
1. Instead of:
Point ThisPoint = createPoint( 1, 1 );

you do:
Point ThisPoint = { 1, 1 };
// That *should* work. If not:
Point ThisPoint;
ThisPoint.X = 1;
ThisPoint.Y = 1;
// That is far more 'efficent'

2. You can't, that's a class. The closest you can do is:
void MovePoint( Point * pPoint, int MoveX, int MoveY )
{
if( pPoint != NULL )
{
pPoint->X += MoveX;
pPoint->Y += MoveY;
}
}

// Usage
Point ThisPoint = { 1, 1 };
MovePoint( &ThisPoint, 10, 15 );

3. For god's sake, just use classes.

CT_Bolt
November 22nd, 2006, 16:51
1 & 2:
Well... I thought that might be what I would have to do... I just wanted to know if there were better ways to do this in 'c'

3. You can't in 'c' but, I would much prefer that.

:cool: OK Well THANK YOU for your help! :D

yaustar
November 22nd, 2006, 19:57
Why are you using C over C++ anyway?

quzar
November 22nd, 2006, 21:41
Just as a point of interest, I think it's always a good idea to relearn how to do things without the programming language's help. This is a relatively poor example, but there are places where you would rather use structs than classes and C over C++.