This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Re: c++ "with" keyword
- From: "Norman Jonas" <normanjonas at arcor dot de>
- To: <gcc at gcc dot gnu dot org>
- Date: Sun, 29 Dec 2002 15:06:23 +0100
- Subject: Re: Re: c++ "with" keyword
I think you missed the point. The reason for the with keyword is not to use
a pointer but to leave
the long structs name which is not done by your example :
struct S
{
char* name;
char* street;
char* city;
} verylongdescriptivename;
If you want to access several values of this struct you always have to type
in the whole name :
verylongdescriptivename.name = "hans";
verylongdescriptivename.street = "xxx 13";
verylongdescriptivename.city = "cologne";
using the "with" keyword this code becomes much smaller and cleaner :
with ( verylongdescriptivename )
{
.name = "hans";
.street = "xxx 13";
.city = "cologne";
}
( It is possible to use a pointer with a very short, undescriptive name, but
that makes the
code unreadable and stupid ( variables should have explanative names, not a
confusing x* )
Norman
> erik wrote :
>
> The example
>
> struct S
> {
> int x;
> int y;
> };
>
> int main()
> {
> S s;
> with (s)
> {
> .x = 1;
> .y = 2;
> }
> return 0;
> }
>
> can easily be rewritten by introducing temporary references, as in
>
> int main()
> {
> S s;
> {
> S& t = s;
> t.x = 1;
> t.y = 2;
> }
> return 0;
> }
>
> This requires only one additional variable reference each time the
> "with" object is used. Additionally, it allows several "with" objects
> (with different names) at the same time. In C, the same thing can be
> done by using pointers.
>
> -erik