John Carson wrote:
"Greg" <gr****@pacbell.net> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com placid wrote:
int main(void)
{
A* aa = new B();
}
Note also the proper syntax to allocate a B object:
int main()
{
A* aa = new B;
...
}
A* aa = new B();
is also correct. See section 5.3.4/15 of the standard. The use of () means
that the object is value initialized.
Yes, the parentheses are legal in this new expression. But my point was
that the parentheses are completely superfluous when allocating an
instance of a class type. The object is default-initialized without the
parentheses, the object is default-initialized with the parentheses, so
their presence in the statement has no effect on its meaning.
The economy of expression principle holds that only those operators
needed to complete an expression should appear in it. In other words,
it is not a good idea to pepper one's code with aesthetically pleasing
operators that otherwise contribute nothing to its meaning. Doing so
makes the program harder to understand, since someone examining the
code must first decide whether an operator in a statement is performing
some necessary operation, or whether it can be excluded from analysis.
More importantly, superfluous operators in one context may suddenly
become meaningful in slightly different contexts and do so in
surprising ways. Even in a new expression, the Standard warns that the
presence of parentheses can have surprising effects (§5.3.4/3) - which
is another argument not to use parentheses indiscriminately in a C++
program, but to use them only on an as-needed basis.
Greg