For Programmers: Free Programming Magazines  


Home > Archive > C > June 2006 > constructor?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author constructor?
Gernot Frisch

2006-06-26, 6:56 pm


Hi,

since C does not support construcor functions for structs, what is the
usual approach to write this C++ code in C:
code:
struct foo { foo() {bar=0;} int bar; }; int main() { foo f[5]; }


I would write:
code:
struct foo { bar; }; Init(foo& f) { bar=0; } int main(int, char**) { foo f[5]; // Put this in a Macro: INIT(f)? for(int i=0; i<sizeof(f)/sizeof(f[0]); ++i) Init(f[i]); return 0; }


Any better ideas?



--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}



Nelu

2006-06-26, 6:56 pm

"Gernot Frisch" <Me@Privacy.net> writes:

> Hi,
>
> since C does not support construcor functions for structs, what is the
> usual approach to write this C++ code in C:
>
code:
> struct foo > { > foo() {bar=0;} > int bar; > }; > > int main() > { > foo f[5]; > } >

>


In C, you usually write initialization function to do the job of C++ constructors.

> I would write:
> [code]
> struct foo
> {
> bar;
> };
>
> Init(foo& f)
> {
> bar=0;
> }
>


There's no passing by reference in C. You'll have to pass a pointer (by-value):

Init(foo *f) {
f->bar=0;
}

f->bar instead of bar, because you don't know where bar belongs.

> int main(int, char**)
> {
> foo f[5];
> // Put this in a Macro: INIT(f)?
> for(int i=0; i<sizeof(f)/sizeof(f[0]); ++i)
> Init(f[i]);
> return 0;
> }


Call the function I wrote above like this:
Init(&f[i]);
instead of
Init(f[i]);
(the reference thing that does not exist in C)

>
> Any better ideas?


It may be good to rename Init to foo_init. Keeps name clashing at bay :-)


--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Mark McIntyre

2006-06-26, 6:57 pm

On Tue, 20 Jun 2006 16:29:50 +0200, in comp.lang.c , "Gernot Frisch"
<Me@Privacy.net> wrote:

>
>Hi,
>
>since C does not support construcor functions for structs, what is the
>usual approach to write this C++ code in C:


struct foo { int bar; };

int main(void)
{
struct foo f[5] = {0};
return 0;
}

This works because if you initialise the first element of an array,
the remainder is set to zero by default.

>Any better ideas?


If you want to do something more complex than initialising the first
element, then you need to switch mindset to C and avoid using C++
models. The languages are different and its not often a good idea to
try to foce one language to emulate another.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com