Why didn't ANSI make initialisation consistent ????
Jamshid Afshar
jamshid at ut-emx.uucp
Sat Apr 27 07:42:38 AEST 1991
In article <959 at agcsun.UUCP> jackm at agcsun.UUCP (Jack Morrison) writes:
>In article <1991Apr24.141206.18103 at grep.co.uk> vic at grep.co.uk (Victor Gavin) writes:
...
>>Which made me believe that I could use the following code:
>> struct bert { int a, b; }
>> struct fred { struct bert *abc; } blip = { {1,1} };
...
>It's not so much an *initialization* inconsistency, but the limitation that
>you can't write a constant structure value the same way you can a constant
>string. For example, it would also be nice to be able to call
>
> void foo(struct bert b);
>
> foo( {1,1} );
>or, if the compiler would like a little more help,
> foo( (struct bert){1,1} );
I just thought I'd mention that this kind of structure-on-the-fly stuff
is possible in C++.
struct bert {
int a, b;
bert(int the_a, int the_b) : a(the_a), b(the_b) {}
};
foo( bert b ); // you don't need the keyword 'struct'
...
foo( bert(5, 6) ); // call foo() with temporary bert
Yes, it's a temporary created on the stack, but you can make a static
object:
static bert a_bert = bert(5, 6);
or static bert a_bert(5, 6); // these two definitions are equivalent
But, as Jack said, you can't write a constant structure value the same
way you can a constant string in C or C++ (can't gcc do something like
this, though?). Therefore, there is no way to do what Victor wanted
to do without another variable.
Anyway, I think every C programmer should take a look at C++, even if
she or he doesn't do object-oriented programming (though I have found
OOP to be a Very Good Thing). C++ removes alot of restrictions on
initializations, has type-safe i/o (doesn't use var. args), is more
strongly typed, has type-safe linkage, and IMO is just a better C. An
ANSI C program should compile without any changes (except C++ requires
a cast when assigning a void pointer to a typed pointer).
Just another cog in the wheel of the OOP propaganda machine, :)
Jamshid Afshar
jamshid at emx.utexas.edu
More information about the Comp.lang.c
mailing list