Topic: Q: With statement in C++ ?


Author: aGriffiths@ma.ccngroup.com (Alan Griffiths)
Date: 1997/09/19
Raw View

In article: <01bcc3e5$b2f10bc0$203174cf@worldnet.worldnet.att.net>  "Cristian Georgescu" <Cristian.Georgescu@worldnet.att.net> writes:
 >
 >
 > Q: With statement in C++ ?
 > ================
 >
 > Is there a way to mimic a with statement in c++:
 >
 > Instead of:
 >
 > a.b.c.action1();
 > a.b.c.action2();
 > a.b.c.action3();
 > a.b.c.action4();
 >
 > Write:
 >
 > with a.b.c {
 >  .action1();
 >  .action2();
 >  .action3();
 >  .action4();
 > }

Yes, let C::action[1-4] return *this...

a.b.c.action1()
     .action2()
     .action3()
     .action4();

But I doubt that client code should access b or c anyway. :)

__
Alan Griffiths              | Senior Systems Consultant, Experian (Formerly CCN)
alan.griffiths@experian.com (agriffiths@ma.ccngroup.com, Tel. +44 115 934 4517)
(ACCU C++ SIG organiser     | <http://www.accu.org/> alan@octopull.demon.co.uk)
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Cristian Georgescu" <Cristian.Georgescu@worldnet.att.net>
Date: 1997/09/18
Raw View
Q: With statement in C++ ?
================

Is there a way to mimic a with statement in c++:

Instead of:

a.b.c.action1();
a.b.c.action2();
a.b.c.action3();
a.b.c.action4();

Write:

with a.b.c {
 .action1();
 .action2();
 .action3();
 .action4();
}


--
Cristian Georgescu
_________________________________________________
    Smith Industries
    Aerospace & Defense Systems
    7-9 Vreeland Road,
    Florham Park, NJ 07932, USA.
_________________________________________________
E-mail: Georgescu_Christian@si.com
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: hnsngr@sirius.com (Ron Hunsinger)
Date: 1997/09/18
Raw View
In article <01bcc3e5$b2f10bc0$203174cf@worldnet.worldnet.att.net>,
"Cristian Georgescu" <Cristian.Georgescu@worldnet.att.net> wrote:

> Q: With statement in C++ ?
> ================
>
> Is there a way to mimic a with statement in c++:
>
> Instead of:
>
> a.b.c.action1();
> a.b.c.action2();
> a.b.c.action3();
> a.b.c.action4();
>
> Write:
>
> with a.b.c {
>         .action1();
>         .action2();
>         .action3();
>         .action4();
> }

Sure. Let T be the type of a.b.c. Then write:

    {
        T& t = a.b.c;
        t.action1();
        t.action2();
        t.action3();
        t.action4();
    }

This should generate identical code to what the Pascal 'with' statement
generated (even if "a.b.c" was something more complicated that would have
prevented the C++ compiler from generating the same code anyway), is nearly
as terse as the Pascal source, and a lot less error-prone in the presence
of nested 'with's.

-Ron Hunsinger
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]