Simulating Classes in C Using Structs

C

Chris Lieb

I am trying to create a "pseudo-class" using structs. Member variables
seem easy enough to do. However, trying to implement member functions
has led to nothing but frustration. I have tried using function
pointers, but when I do, I cannot figure out how to get access to the
member variables. Here is what I have so far:

typedef struct {
int board[8][8];
void (*toString)();
void (*clear)();
} BOARD;

void printBoard() {
// doing stuff with board
}

void clearBoard() {
// doing stuff with board
}

BOARD newBoard() {
BOARD out;

out.clear = clearBoard;
out.toString = printBoard;

return out;
}

When I try to compile (using GCC), I get "error: 'board' undeclared
(first use in this function)".

Is there a way to accomplish this?

Thanks in advance,
Chris Lieb
 
B

Ben Pfaff

Chris Lieb said:
I am trying to create a "pseudo-class" using structs. Member variables
seem easy enough to do. However, trying to implement member functions
has led to nothing but frustration. I have tried using function
pointers, but when I do, I cannot figure out how to get access to the
member variables. Here is what I have so far:

typedef struct {

typedef struct board
int board[8][8];
void (*toString)();

void (*toString)(struct board *);
void (*clear)();

void (*clear)(struct board *);
} BOARD;

void printBoard() {

void printBoard(struct board *board) {
// doing stuff with board
}

void clearBoard() {

void clearBoard(struct board *board) {
// doing stuff with board
}

BOARD newBoard() {
BOARD out;

out.clear = clearBoard;
out.toString = printBoard;

return out;
}

It is unusual to try to return a relatively large object by-value
this way. I'd suggest either passing in the address of an object
to initialize as a parameter, or allocating a new object
dynamically within the function and returning the allocated
object.
 
L

Laurent Deniau

Chris said:
I am trying to create a "pseudo-class" using structs. Member variables
seem easy enough to do. However, trying to implement member functions
has led to nothing but frustration. I have tried using function
pointers, but when I do, I cannot figure out how to get access to the
member variables. Here is what I have so far:

typedef struct {
int board[8][8];
void (*toString)();
void (*clear)();
} BOARD;

void printBoard() {
// doing stuff with board
}

void clearBoard() {
// doing stuff with board
}

BOARD newBoard() {
BOARD out;

out.clear = clearBoard;
out.toString = printBoard;

return out;
}

When I try to compile (using GCC), I get "error: 'board' undeclared
(first use in this function)".

Is there a way to accomplish this?

The very basic code below (untested) should do what you are looking for.
And from this point, implementing (manually) inheritance should be easy.
As an introduction to this topic, you may have a look at:

http://cern.ch/laurent.deniau/html/oopc/oopc.html

a+, ld.

// -------
// board.h

#ifndef BOARD_H
#define BOARD_H

struct board {
struct board_interface *_i;
int board[8][8];
};

struct board_interface {
void (*del)(struct board*);
void (*toString)(struct board*);
};

struct board* new_board(void);
struct board_interface* new_board_interface(void);

#endif

// -------
// board.c

#include <stdlib.h>
#include "board.h"

struct board_interface*
new_board_interface(void)
{
static struct board_interface ib = {0};

if (!ib.del) {
// set pointers of interface to corresponding (local) implementation
// ib.del = del; // local (static) function
// ib.toString = toString; // local (static) function
}

return &ib;
}

struct board*
new_board(void)
{
struct board *b = malloc(sizeof *b);
// check b != 0.
b->_i = new_board_interface();
// init of b->board
return b;
}

// -------
// main.c

#include "board.h"

int main(void)
{
struct board *b = new_board();

b->_i->toString(b);
b->_i->del(b);

return 0;
}
 
B

Barry Schwarz

I am trying to create a "pseudo-class" using structs. Member variables
seem easy enough to do. However, trying to implement member functions
has led to nothing but frustration. I have tried using function
pointers, but when I do, I cannot figure out how to get access to the
member variables. Here is what I have so far:

typedef struct {
int board[8][8];
void (*toString)();
void (*clear)();

If you intend to pass arguments to the functions these pointers point
to, which seems likely, then you should define the struct members with
the correct parameter types.
} BOARD;

void printBoard() {
// doing stuff with board
}

void clearBoard() {
// doing stuff with board
}

BOARD newBoard() {
BOARD out;

out.clear = clearBoard;
out.toString = printBoard;

return out;
}

When I try to compile (using GCC), I get "error: 'board' undeclared
(first use in this function)".

C is case sensitive. The error message refers to "board". You have a
typedef for "BOARD". They are not the same.


Remove del for email
 
J

Jeff Mullen

Chris said:
I am trying to create a "pseudo-class" using structs. Member variables
seem easy enough to do. However, trying to implement member functions
has led to nothing but frustration. I have tried using function
pointers, but when I do, I cannot figure out how to get access to the
member variables. Here is what I have so far:

typedef struct {
int board[8][8];
void (*toString)();
void (*clear)();
} BOARD;

void printBoard() {
// doing stuff with board
}

void clearBoard() {
// doing stuff with board
}

BOARD newBoard() {
BOARD out;

out.clear = clearBoard;
out.toString = printBoard;

return out;
}

When I try to compile (using GCC), I get "error: 'board' undeclared
(first use in this function)".

Is there a way to accomplish this?

Thanks in advance,
Chris Lieb

Regardless of your other issues, you've got a major problem here.
In newboard(), you've declared a local variable, out, which is put
on the stack. When you exit the function, the memory that holds
that structure is disposed of. Thus, you are returning a hyperspace
pointer. To make the function work as advertised, you need to
declare out as a

static BOARD

OR make it global OR allocate it dynamically (two other posts have
dealt with this option, one explicitly and one through example, though
the one with the example engages in the sloppy practice of allocating
a structure and not freeing it).

For the record, I have no idea where your compiler error is coming from.

I took your code as is, with no include files added or anything, and
compiled it under MSVC 6, and it was just fine. I then added a line
to your newBoard() function to access the board member of the structure,

out.board[0][0] = 1;

and that ALSO compiled with neither warning nor error.

Good luck.

Jeff
 
K

Keith Thompson

Jeff Mullen said:
Chris said:
I am trying to create a "pseudo-class" using structs. Member variables
seem easy enough to do. However, trying to implement member functions
has led to nothing but frustration. I have tried using function
pointers, but when I do, I cannot figure out how to get access to the
member variables. Here is what I have so far:
typedef struct {
int board[8][8];
void (*toString)();
void (*clear)();
} BOARD;
void printBoard() {
// doing stuff with board
}
void clearBoard() {
// doing stuff with board
}
BOARD newBoard() {
BOARD out;
out.clear = clearBoard;
out.toString = printBoard;
return out;
}
When I try to compile (using GCC), I get "error: 'board' undeclared
(first use in this function)".
Is there a way to accomplish this?
Thanks in advance,
Chris Lieb

Regardless of your other issues, you've got a major problem here.
In newboard(), you've declared a local variable, out, which is put
on the stack. When you exit the function, the memory that holds
that structure is disposed of. Thus, you are returning a hyperspace
pointer.

No, he's not returning a pointer at all. In the newBoard function,
"out" is an object of type BOARD (a struct); "return out;" returns the
*value* of the structure, not its address.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,219
Messages
2,571,118
Members
47,737
Latest member
CarleyHarm

Latest Threads

Top