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...@si.com
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std...@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++...@ncar.ucar.edu ]
> 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
In article: <01bcc3e5$b2f10bc0$2031...@worldnet.worldnet.att.net> "Cristian Georgescu" <Cristian....@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.gr...@experian.com (agrif...@ma.ccngroup.com, Tel. +44 115 934 4517)
(ACCU C++ SIG organiser | <http://www.accu.org/> al...@octopull.demon.co.uk)
but take a look:
// --- "sample.cpp"
struct C
{
int action1(){ return 1; }
int action2(){ return 2; }
int action3(){ return 3; }
};
struct B
{
C c;
};
struct A
{
B b;
};
void f(void)
{
A a;
C& rc = a.b.c;
rc.action1(); // same as a.b.c.action1()
rc.action2(); // same as a.b.c.action2()
rc.action3(); // same as a.b.c.action3()
}
hope it helps,
Augustin MOGA (aka { t i T i } )
Cristian Georgescu wrote in article
<01bcc3e5$b2f10bc0$2031...@worldnet.worldnet.att.net>...
>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...@si.com
>---
>[ comp.std.c++ is moderated. To submit articles: try just posting
]
>[ your news-reader. If that fails, use
>[ 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?
ucar.edu ]
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std...@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++...@ncar.ucar.edu
]