473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to best have c++ class-style structs?

Hi,

In my programming I do something like,

struct my_struct {
int data1;
int data1;
static void (* const my_privatefunc1) (my_struct * this)
static void (* const my_privatefunc2) (my_struct * this)
}
// function definitions
static void my_privatefunc1(mystruct * this) { }
static void my_privatefunc2(mystruct * this) { }

And on initialisation I do:

struct my_struct x = {
0,
0,
my_privatefunc1,
my_privatefunc2
};

if I had an init function for this, I would best have one that
allocated, initialised and returned a new object from heap, i.e.
my_struct * xp = x_allocator_and_initialisor();

or if I want my object on the stack, I could have a loose init function
and a struct on stack, i.e.
my_struct x; x_initialisor(&x);

But, whatif I wanted a statically allocated object, yet initialised,
and client involved in neither setting values explicitly, nor
associating it with such an init function. i.e. if I could do;

my_struct x; x.init(); that would be nice, because I dont want to have
the loose practices above. But would it be possible to associate init()
with x like this, in the first place?

Cheers,
Bahadir

Nov 14 '05 #1
4 1401
ba************@gmail.com wrote:
Hi,

In my programming I do something like,

struct my_struct {
int data1;
int data1;
static void (* const my_privatefunc1) (my_struct * this)
static void (* const my_privatefunc2) (my_struct * this)
}
// function definitions
static void my_privatefunc1(mystruct * this) { }
static void my_privatefunc2(mystruct * this) { }

And on initialisation I do:

struct my_struct x = {
0,
0,
my_privatefunc1,
my_privatefunc2
};

if I had an init function for this, I would best have one that
allocated, initialised and returned a new object from heap, i.e.
my_struct * xp = x_allocator_and_initialisor();

or if I want my object on the stack, I could have a loose init function
and a struct on stack, i.e.
my_struct x; x_initialisor(&x);

But, whatif I wanted a statically allocated object, yet initialised,
and client involved in neither setting values explicitly, nor
associating it with such an init function. i.e. if I could do;

my_struct x; x.init(); that would be nice, because I dont want to have
the loose practices above. But would it be possible to associate init()
with x like this, in the first place?

Cheers,
Bahadir


There is no way in C to call an initialization function before
the main() function runs.

One work-around is to call an initialiaztion function as the first
procedure called by main(). That function would call all other
initializations necessary for all objects you build.

Another is to assign to your objects statically their value:

static my_struct myobject = {
0, 25, myfn1,myfn2};

and be done with it :-)

jacob
Nov 14 '05 #2
You cannot Statically allocate a dynamically allocated resource. That is
you can Statically or Dynamically allocate, not both.

Some compilers do allow for code execution before main. I'm not sure if it
is a standard. ( one of these days I'll download it.)

It should be found in your documentation about the same place as atexit();

<ba************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,

In my programming I do something like,

struct my_struct {
int data1;
int data1;
static void (* const my_privatefunc1) (my_struct * this)
static void (* const my_privatefunc2) (my_struct * this)
}
// function definitions
static void my_privatefunc1(mystruct * this) { }
static void my_privatefunc2(mystruct * this) { }

And on initialisation I do:

struct my_struct x = {
0,
0,
my_privatefunc1,
my_privatefunc2
};

if I had an init function for this, I would best have one that
allocated, initialised and returned a new object from heap, i.e.
my_struct * xp = x_allocator_and_initialisor();

or if I want my object on the stack, I could have a loose init function
and a struct on stack, i.e.
my_struct x; x_initialisor(&x);

But, whatif I wanted a statically allocated object, yet initialised,
and client involved in neither setting values explicitly, nor
associating it with such an init function. i.e. if I could do;

my_struct x; x.init(); that would be nice, because I dont want to have
the loose practices above. But would it be possible to associate init()
with x like this, in the first place?

Cheers,
Bahadir

Nov 14 '05 #3
ba************@gmail.com wrote:
In my programming I do something like,

struct my_struct {
int data1;
int data1;
static void (*const my_privatefunc1) (my_struct * this)
static void (*const my_privatefunc2) (my_struct * this)
}
// function definitions
static void my_privatefunc1(mystruct * this) { }
static void my_privatefunc2(mystruct * this) { }

// And on initialisation I do:

struct my_struct x = {
0,
0,
my_privatefunc1,
my_privatefunc2
};
This is completely bogus:
gcc -Wall -std=c99 -pedantic -S my_struct.c my_struct.c:4: error: parse error before "static"
my_struct.c:4: warning: no semicolon at end of struct or union
my_struct.c:8: error: parse error before '*' token
my_struct.c:9: error: parse error before '*' token
my_struct.c:11: error: parse error before "on"
my_struct.c:18: warning: ISO C does not allow extra `;' \
outside of a function
my_struct.c:8: warning: 'my_privatefunc1' defined but not used
my_struct.c:9: warning: 'my_privatefunc2' defined but not used
If I had an init function for this,
I would best have one that
allocated, initialised and returned a new object from heap, i.e.

my_struct * xp = x_allocator_and_initialisor();

or, if I want my object on the stack,
I could have a loose init function and a struct on stack, i.e.

my_struct x; x_initialisor(&x);

But, whatif I wanted a statically allocated object, yet initialised,
and client involved in neither setting values explicitly, nor
associating it with such an init function. i.e. if I could do;

my_struct x; x.init();

that would be nice, because I don't want to have the loose practices above.
But would it be possible to associate init() with x like this, in the first place?
Try this:
cat my_struct.h #ifndef GUARD_MY_STRUCT_H
#define GUARD_MY_STRUCT_H 1

#include <stdio.h>

typedef struct my_struct {
const
void* const pVFT;
int data1;
int data2;
} my_struct;

// virtual functions
void my_struct_func1(const my_struct* p);
void my_struct_func2(const my_struct* p);

// constructors
my_struct my_struct_create(int d1, int d2);
void my_struct_destroy(const my_struct* p);

#endif//GUARD_MY_STRUCT_H
cat my_struct.c #include "my_struct.h"

// function definitions
static void my_privatefunc1(const my_struct* p) {
fprintf(stderr, "my_privatefunc1(const my_struct*): "
"data1 = %d\n", p->data1);
}
static void my_privatefunc2(const my_struct* p) {
fprintf(stderr, "my_privatefunc2(const my_struct*): "
"data2 = %d\n", p->data2);
}

typedef struct my_struct_virtualFunctionTable_t {
void (* const my_privatefunc1)(const struct my_struct*);
void (* const my_privatefunc2)(const struct my_struct*);
} my_struct_virtualFunctionTable_t;

my_struct_virtualFunctionTable_t my_struct_VFT = {
my_privatefunc1,
my_privatefunc2
};

// virtual functions
void my_struct_func1(const my_struct* p) {
((my_struct_virtualFunctionTable_t*)(p->pVFT))
->my_privatefunc1(p);
}
void my_struct_func2(const my_struct* p) {
((my_struct_virtualFunctionTable_t*)(p->pVFT))
->my_privatefunc2(p);
}

// constructors
my_struct my_struct_create(int d1, int d2) {
my_struct x = { (void*)(&my_struct_VFT), d1, d2 };
return x;
}
void my_struct_destroy(const my_struct* p) { }
cat main.c #include "my_struct.h"

int main(void) {
const
my_struct t = my_struct_create(13, 42);
my_struct_func1(&t);
my_struct_func2(&t);
my_struct_destroy(&t);
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c my_struct.c
./main

my_privatefunc1(const my_struct*): data1 = 13
my_privatefunc2(const my_struct*): data2 = 42
Nov 14 '05 #4
while not _completely bogus_ yes, there are a few typos, but
functionally the method is correct. Here's a compilable version:

struct my_struct {
int dataA;
int dataB;
void (* const my_privatefunc1) (struct my_struct *);
void (* const my_privatefunc2) (struct my_struct *);

};
static void my_privatefunc1(struct my_struct * this) { }
static void my_privatefunc2(struct my_struct * this) { }
int main (int argc, char * argv[])
{
struct my_struct x = {
0,
0,
my_privatefunc1,
my_privatefunc2
};
}

I think your method is a useful imitation of c++ should one need
virtual functions. But your constructor creates the object on the stack
and returns it. So won't you have problems with that?

Bahadir

Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Paul | last post by:
Hi. Just trying to find out the best approach as I beleive it might give me problems later on down the road. I have an ASP.NET application which references a shared database class which...
15
by: John J | last post by:
I've written the following code into a class to search for and display the results of all races entered (The complete code is in a previous thread). I wish to amend the code so as to display the...
4
by: Amadelle | last post by:
Hi all and thanks again in advance, What is the best way of defining global constants in a C# application? (A windows application with no windows forms - basically a set of classes). Would it be...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
14
by: Jon Rea | last post by:
I am currently cleaning up an application which was origainlly hashed together with speed of coding in mind and therefore contains quite a few "hacky" shortcuts. As part of this "revamping"...
1
by: DelphiLover | last post by:
Hi. I'm reading and reading, testing and testing, trying to figure out how to do things, how to do things according to best practises and how to do things in the best object oriented way. ...
8
by: Carl Heller | last post by:
If I'm creating a class to do some work that I want threaded out, where's the best location to call ThreadStart? Or does it depend on the nature of the work? a. Call it outside the class,...
5
by: gw7rib | last post by:
I'm writing a program which has "notes" - these can appear on the screen as windows with text in. It is possible to create an "index note" - at present, this will contain a list of the titles (or...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
41
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.