By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,407 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,407 IT Pros & Developers. It's quick & easy.

constructor?

P: n/a

Hi,

since C does not support construcor functions for structs, what is the
usual approach to write this C++ code in C:
Expand|Select|Wrap|Line Numbers
  1. struct foo
  2. {
  3. foo() {bar=0;}
  4. int bar;
  5. };
  6.  
  7. int main()
  8. {
  9. foo f[5];
  10. }
  11.  
I would write:
Expand|Select|Wrap|Line Numbers
  1. struct foo
  2. {
  3. bar;
  4. };
  5.  
  6. Init(foo& f)
  7. {
  8. bar=0;
  9. }
  10.  
  11. int main(int, char**)
  12. {
  13. foo f[5];
  14. // Put this in a Macro: INIT(f)?
  15. for(int i=0; i<sizeof(f)/sizeof(f[0]); ++i)
  16. Init(f[i]);
  17. return 0;
  18. }
  19.  
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);}

Jun 26 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"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:
Expand|Select|Wrap|Line Numbers
  1.  struct foo
  2.  {
  3.      foo() {bar=0;}
  4.      int bar;
  5.  };
  6.  int main()
  7.  {
  8.      foo f[5];
  9.  }
  10.  

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...)
Jun 26 '06 #2

P: n/a
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
Jun 26 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.