By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,280 Members | 2,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,280 IT Pros & Developers. It's quick & easy.

Simulating Classes in C Using Structs

P: n/a
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

Jan 19 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Chris Lieb" <ch********@gmail.comwrites:
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.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
Jan 19 '07 #2

P: n/a
Chris Lieb wrote:
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;
}
Jan 19 '07 #3

P: n/a
On 19 Jan 2007 09:52:32 -0800, "Chris Lieb" <ch********@gmail.com>
wrote:
>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
Jan 20 '07 #4

P: n/a
Chris Lieb wrote:
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
Jan 22 '07 #5

P: n/a
Jeff Mullen <41*@nls.netwrites:
Chris Lieb wrote:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 22 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.