<yp*********@indiatimes.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
int i = aa.b(); is not a statement.
Ok .agreed.
but i can straight away ignore the value of i and i can achieve the
same thing which i could have achieved with the mere aa.b() statement.
and I can make the compiler a fool.
regards,
Yogesh Joshi
I doubt the compiler cares what anyone thinks of it. :-)
But you are correct (if I understand what you're trying to say), in that
this form of variable initialization allows you to execute code which you'd
otherwise be prevented from doing outside of a function. But C++ *needs*
the ability to do this, or else you'd have to default initialize everything
that was globally declared (as well as static member variable
initializations).
The restriction isn't there because the compiler doesn't want you executing
code from outside of functions, it's there because, aside from variable
initializations, it simply makes no sense to put executable statements
outside of functions. When and in what order would they be executed?
Your method of initializing a variable in order to execute some code can be
useful in some circumstances, where it's really the side-effects of that
initialization that are important to you. But you could also just add those
side effects to the constructor of the object, not to a member function.
Then there'd be no need to declare a "dummy" global variable just to get the
side effects of its initialization.
-Howard