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 5 2534
"Chris Lieb" <ch********@gma il.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)(str uct board *);
void (*clear)();
void (*clear)(struct board *);
} BOARD;
void printBoard() {
void printBoard(stru ct board *board) {
// doing stuff with board
}
void clearBoard() {
void clearBoard(stru ct 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
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)(str uct board*);
};
struct board* new_board(void) ;
struct board_interface * new_board_inter face(void);
#endif
// -------
// board.c
#include <stdlib.h>
#include "board.h"
struct board_interface *
new_board_inter face(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_inter face();
// 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;
}
On 19 Jan 2007 09:52:32 -0800, "Chris Lieb" <ch********@gma il.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
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
Jeff Mullen <41*@nls.netwri tes:
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_Keit h) 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Brad Tilley |
last post by:
I don't understand classes very well... maybe some day. Is it just me or
are they supposed to be difficult to understand? They make my head hurt.
Anyway, because I don't understand classes well, I avoid using them.
However, many modules in the standard library are implemented as
classes: sgmllib, HTMLParser, etc. Is it possible to use these modules
without getting into OO programming and inheritance and all the other
lofty, theoretical...
|
by: Pratts |
last post by:
I am a new one who have joined u plz try to help me bcoz i could not
find ny sutiable answer foer this Question
Qus>>why do we need classes when structures provide similar
functionality??
|
by: nick |
last post by:
I have tried finding an answer to this, but most people just explain
classes as a more modular way to program. It seems to me that
(forgetting OO programming which I don't quite understand) the
structures in C are the same as classes in another language such as C++
or Java only missing the ability to make the data private.
I've never had this explained sufficiently and would appreciate a good
answer. It doesn't need to be Mickey Mouse,...
|
by: Bill |
last post by:
I would like to create a static array of classes (or structs) to be used in
populating name/value pairs in various WebForm drop down list boxes, but am
not quite sure of the construct (or rather to use structs instead of classes
in the array insofar as structs vs. classes appears to be controversial in
C# -- with some recommending avoiding structs altogether).
It needs to be an array something like this:
struct NoteValue
{
|
by: Bilgehan.Balban |
last post by:
Hi,
I am currently brushing up my c++ knowledge and I would like to ask you
about the differences between classes and C structs, in the
function/method perspective.
1) Is it correct to say that, a structure definition that includes
function pointers only defines the function prototypes to be used with
them, but not the actual implementations, whereas in C++, member
functions cannot be changed *unless* virtual functions are used, or the
| |
by: Markus Svilans |
last post by:
Hi,
What is the difference between having a struct with constructors and
methods versus a class?
In many C++ examples online I have seen code similar to this:
struct Animal
{
Animal()
|
by: giddy |
last post by:
hi ,
i'm a C / C# programmer .. have'nt done C++,
in C# .. . object instances of classes ( TextBox txt = new TextBox();)
are reference types. Structs on the other hand are value types.
In C++ i knw there are a few difference between classes and structs but
i need to know if there are value or refrence types.
|
by: Dom |
last post by:
I'm really confused by the difference between a Struct and a Class?
Sometimes, I want just a group of fields to go together. A Class
without methods seems wrong, in that it carries too much overhead (I
think). A Struct seems more appropriate. At least it is what I would
have used in other languages. But since a Struct *can* hold methods,
I wander if I am saving anything. If not, why use it?
|
by: desktop |
last post by:
There is a lot of info on this topic on google. But in Bjarne Stroustrup
's book page 225 he writes:
"The declaration of Date in the previous subsection (declared as a
struct) provides a set of functions for manipulating a Date. However, it
does not specify that those functions should be the only ones to depend
directly on Date ’s representation and the only ones to directly access
objects of class Date . This restriction
can be...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |