Re: Enum oddity
On Aug 24, 8:18 pm, Jerry Coffin <jerryvcof...@yahoo.com> wrote:
In article <80aa8010-71b2-416f-af00-ad0f7f33bce8@
32g2000yqj.googlegroups.com>, entul...@gmail.com says...
On 24 Ago, 19:06, Juha Nieminen <nos...@thanks.invalid> wrote:
Francesco wrote:
Color col(Color::color(i));
The problem is that that line is not doing what you think
it's doing. You think it's creating an instance of the
class Color named 'col'. In reality you are declaring a
function named 'col'.
Yes, this is an oddity in C++, probably some semantical
leftover from C. Extra parentheses will disambiguate the
expression to mean what you want it to mean.
Thank you for your reply Juha, yes, finally I've got this
straight - that's what I meant with "Freakin' parentheses"
;-)
Actually, I knew this issue but I didn't recognize it - as
you might guess, I thought the compiler was broken ;-) - now
that I hit this obstacle in my actual code (that's the first
time), chances are I'll recognize it the next time - fingers
crossed :-/
Unfortunately, chances are that it'll still bite you, at least
once in a while.
It still bites me from time to time, and that's after twenty
years experience with C++.
One obvious one is if you have something like this:
std::copy(std::istream_iterator(std::cin),
std::istream_iterator(),
std::back_inserter(my_vector));
Now, this works, but it's quite long, so you might want to break it
up into some pieces that are more readable, like this:
std::istream_iterator input(std::cin);
std::istream_iterator end();
std::copy(input, end, std::back_inserter(my_vector));
There's just one minor problem: even though you did
essentially the same thing to define 'input' and 'end', end
isn't really a definition at all -- since the parens are
empty, it's really a declaration of a function named end that
returns a std::istream_iterator and takes no parameters.
The case I run into the most is when I want to initialize the
array directly, instead of using copy:
std::vector< int > my_vector(
std::istream_iterator< int >( std::cin ),
std::istream_iterator< int >() ) ;
Stupid compiler doesn't realize that if I'm naming it
"my_vector", then I don't want a function. (And I use this
idiom a lot, since if I'm not modifying the vector later, it
allows me to declare it const.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34