Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Am I being too C++ like?

Reply
Thread Tools

Am I being too C++ like?

 
 
Joe keane
Guest
Posts: n/a
 
      01-19-2012
In article <(E-Mail Removed)>,
nroberts <(E-Mail Removed)> wrote:
>I've seen both of these used a lot in various C projects, but has it
>gone out of style or something?


It's just good programming.

Good C code resembles C++ written in C.
Bad C code resembles fettucine alfredo.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-20-2012
"BartC" <(E-Mail Removed)> writes:
> "Jorgen Grahn" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed). ..
>> On Wed, 2012-01-18, nroberts wrote:

>
>>> struct stack;
>>> typedef struct stack Stack;

>
>> I'd skip the typedef -- if something is a struct, I want to see
>> that immediately. People typedef the weirdest things, so I feel I
>> cannot assume such a name is a struct, and not a pointer or something.

>
> Couldn't you make exactly the same argument for *anything* defined with
> typedef?


Not quite.

A typedef for a struct type just creates a new name (like "foo") for
something that already has a name (like "struct foo"). It typically
provides no new information, nor does it hide information that
should be hidden; usually the code that refers to the type already
depends on the fact that it's a struct. (For a truly opaque type,
a typedef makes sense; the standard FILE is an example.)

On the other hand, a typedef for a numeric type is typically
parameterized, and may refer to different types depending on
the platform. Examples from the standard library include size_t
and ptrdiff_t.

Typedefs can also be useful for types that might be overly complex
to write out; for example:

typedef void (*signal_handler)(int);

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      01-20-2012
On 01/19/12 10:53 AM, nroberts wrote:
> 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...


If you had used XView, you wouldn't be asking this question! XView was
my introduction to OO programming in C.

> 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?


You are correct, what you are writing is OO C, not C++ like C. If it
passes its tests and your colleagues understand it, go for it.

--
Ian Collins
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      01-20-2012
"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "BartC" <(E-Mail Removed)> writes:
>> "Jorgen Grahn" <(E-Mail Removed)> wrote in message


>>> I'd skip the typedef -- if something is a struct, I want to see
>>> that immediately. People typedef the weirdest things, so I feel I
>>> cannot assume such a name is a struct, and not a pointer or something.

>>
>> Couldn't you make exactly the same argument for *anything* defined with
>> typedef?

>
> Not quite.
>
> A typedef for a struct type just creates a new name (like "foo") for
> something that already has a name (like "struct foo"). It typically
> provides no new information, nor does it hide information that
> should be hidden;


But if I create a new typedef T, and then declare:

T a;

I can't see at a glance whether T is a number, struct, and anything else.
That seemed to be the point of JG's remark. In this case it *does* hide some
information.

--
bartc

 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      01-20-2012
On Jan 20, 3:03*am, Ian Collins <(E-Mail Removed)> wrote:
>
> You are correct, what you are writing is OO C, not C++ like C. *If it
> passes its tests and your colleagues understand it, go for it.
>

OO C is C++-like C.

C++ provides support for object-orientation, C doesn't. Of course
ultimately a design paradigm is independent of the language used to
implement it, and you can emulate any language wtih any other (hence
the claim sometimes made that every sufficiently large project
contains a bug-ridden and informally specified implementation of
Lisp). But it's a lot easier to use languages for the purposes for
which they are intended.

--
Visit my website
http://www.malcolmmclean.site11.com/www
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-20-2012
On 1/20/2012 8:44 AM, BartC wrote:
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> "BartC" <(E-Mail Removed)> writes:
>>> "Jorgen Grahn" <(E-Mail Removed)> wrote in message

>
>>>> I'd skip the typedef -- if something is a struct, I want to see
>>>> that immediately. People typedef the weirdest things, so I feel I
>>>> cannot assume such a name is a struct, and not a pointer or something.
>>>
>>> Couldn't you make exactly the same argument for *anything* defined with
>>> typedef?

>>
>> Not quite.
>>
>> A typedef for a struct type just creates a new name (like "foo") for
>> something that already has a name (like "struct foo"). It typically
>> provides no new information, nor does it hide information that
>> should be hidden;

>
> But if I create a new typedef T, and then declare:
>
> T a;
>
> I can't see at a glance whether T is a number, struct, and anything
> else. That seemed to be the point of JG's remark. In this case it *does*
> hide some information.


Not very effectively, since you need to rediscover at least
some of the "hidden" information before you can use `a'.

a = 42; // legal?
*a |= 42; // legal?
a.fizz = 42; // legal?
a->buzz = 42; // legal?
puts(a); // legal?
longjmp(a, 42); // legal?
a(42); // legal?

As a matter of personal taste I rather like typedef aliases
for struct types (and less often, union types), and I intensely
dislike them for data pointer types (but sometimes use them for
function pointers). For scalar types, I use typedef not so much
for opacity but for portability, using <limits.h> and #if to find
a type with suitable range and publishing the result as a typedef
name. But these are just my preferences, not binding on anyone
and not enshrined in a style standard.

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      01-20-2012
On Jan 20, 2:08*pm, Eric Sosman <(E-Mail Removed)> wrote:
> As a matter of personal taste I rather like typedef aliases
> for struct types (and less often, union types), and I intensely
> dislike them for data pointer types (but sometimes use them for
> function pointers).
>

Say we want an array of ten function pointers that return int and take
a void *.

the syntax is

int (*myftable[10])(void *ptr);

that's really too difficult.

typedef int (*myfpointer)(void *ptr);

myfpointer myftable[10];

is a lot easier to understand.
--
Visit Malcolm's website
http://www.malcolmmclean.site11.com/www
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      01-20-2012
On 01/21/12 03:01 AM, Malcolm McLean wrote:
> On Jan 20, 3:03 am, Ian Collins<(E-Mail Removed)> wrote:
>>
>> You are correct, what you are writing is OO C, not C++ like C. If it
>> passes its tests and your colleagues understand it, go for it.
>>

> OO C is C++-like C.


No, it's OO C. OO C was around before we had C++.

> C++ provides support for object-orientation, C doesn't. Of course
> ultimately a design paradigm is independent of the language used to
> implement it, and you can emulate any language wtih any other (hence
> the claim sometimes made that every sufficiently large project
> contains a bug-ridden and informally specified implementation of
> Lisp). But it's a lot easier to use languages for the purposes for
> which they are intended.


The key point is because of a lack of language support, OO C doesn't
look like C++. The question could have been "Am I being too Java like?"

--
Ian Collins
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-20-2012
Eric Sosman <(E-Mail Removed)> writes:
[...]
> As a matter of personal taste I rather like typedef aliases
> for struct types (and less often, union types), and I intensely
> dislike them for data pointer types (but sometimes use them for
> function pointers).


[...]

You could typedef the function type itself rather than the
pointer-to-function type:

typedef void signal_handler(int);

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Kleuske
Guest
Posts: n/a
 
      01-20-2012
On Fri, 20 Jan 2012 09:08:01 -0500, Eric Sosman saw fit to publish the
following:

> On 1/20/2012 8:44 AM, BartC wrote:


<snip>

>> But if I create a new typedef T, and then declare:
>>
>> T a;
>>
>> I can't see at a glance whether T is a number, struct, and anything
>> else. That seemed to be the point of JG's remark. In this case it
>> *does* hide some information.

>
> Not very effectively, since you need to rediscover at least
> some of the "hidden" information before you can use `a'.
>
> a = 42; // legal?
> *a |= 42; // legal?
> a.fizz = 42; // legal?
> a->buzz = 42; // legal?
> puts(a); // legal?
> longjmp(a, 42); // legal?
> a(42); // legal?
>
> As a matter of personal taste I rather like typedef aliases
> for struct types (and less often, union types), and I intensely dislike
> them for data pointer types (but sometimes use them for function
> pointers). For scalar types, I use typedef not so much for opacity but
> for portability, using <limits.h> and #if to find a type with suitable
> range and publishing the result as a typedef name. But these are just
> my preferences, not binding on anyone and not enshrined in a style
> standard.


In many cases a well_chosen name can actually _add_ some information. Consider
something like:

typedef unsigned int ipaddr_t;

and speculate what variables of this type are used for.

PS. Target platform allows the assumption sizeof(unsigned int)==4.

--
Four thousand throats may be cut in one night by a running man.
-- Klingon Soldier, "Day of the Dove", stardate unknown
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Path Too Long Exception being thrown before HTTPHandler gets request Seth ASP .Net 3 01-02-2011 12:33 AM
Java & LAMP - being better or being popular ? heather.fraser@gmail.com Java 14 10-17-2007 03:50 AM
Java Interview Questions: Am I Being Too Difficult? Spammay Blockay Java 69 04-15-2006 04:08 AM
Form still being submitted despite being invalid =?Utf-8?B?TWFyayBQYXJ0ZXI=?= ASP .Net 4 07-25-2005 02:46 PM
Event handler is being detached without being released Moshe Katz Javascript 2 05-02-2004 06:42 AM



Advertisments