N
nroberts
I'm working on a C project and I come from C++ background. I used to
work with C 10 years ago though when I was first learning various
languages and used things like Xlib, Xt, etc...
I keep getting told that what I write is more C++ like. I find it odd
because I'm actually using C keywords I rarely if ever use in C++ like
union. But two main examples come to mind:
First, I use opaque types to encapsulate data so I can depend on the
integrity of my structures. For example, when making a stack I had a
header like so:
struct stack;
typedef struct stack Stack;
Stack* createList();
void* popFront(Stack*);
void pushBack(Stack*);
etc...
Another example is when I'm parsing an unknown string into one of
several possibilities:
typedef struct base
{
int type;
// shared data among all valid inputs
} Base;
typedef struct variant_a
{
Base base;
// particular data members
} VariantA;
.... more variants...
typedef union blob
{
int type;
Base base;
VariantA a;
...etc...
} Blob;
Blob * parse(char const*);
I've seen both of these used a lot in various C projects, but has it
gone out of style or something? I suppose it's kind of OOesque, but
I've always thought of that as one paradigm for use in any
language...not something unique to any particular one. Is that an
unpopular conception? If I were to do something similar in C++ it
would look absolutely nothing like the above...why do people think
it's C++ like? Are they full of it or am I? Both? Neither?
work with C 10 years ago though when I was first learning various
languages and used things like Xlib, Xt, etc...
I keep getting told that what I write is more C++ like. I find it odd
because I'm actually using C keywords I rarely if ever use in C++ like
union. But two main examples come to mind:
First, I use opaque types to encapsulate data so I can depend on the
integrity of my structures. For example, when making a stack I had a
header like so:
struct stack;
typedef struct stack Stack;
Stack* createList();
void* popFront(Stack*);
void pushBack(Stack*);
etc...
Another example is when I'm parsing an unknown string into one of
several possibilities:
typedef struct base
{
int type;
// shared data among all valid inputs
} Base;
typedef struct variant_a
{
Base base;
// particular data members
} VariantA;
.... more variants...
typedef union blob
{
int type;
Base base;
VariantA a;
...etc...
} Blob;
Blob * parse(char const*);
I've seen both of these used a lot in various C projects, but has it
gone out of style or something? I suppose it's kind of OOesque, but
I've always thought of that as one paradigm for use in any
language...not something unique to any particular one. Is that an
unpopular conception? If I were to do something similar in C++ it
would look absolutely nothing like the above...why do people think
it's C++ like? Are they full of it or am I? Both? Neither?