Empty strings

Hi all C++ programming fellows
Making some experiments with string objects I've seen what I think is a confussing behaviour.
If you want to create an empty string, you have this option (taking from string reference in this web page)

string empty_str (); // No parameters.

Then, if you try this:

cout << ">" << empty_str "<\n"

you get an "1" (the number one)

¿¿???.

If you repeat this but using

string empty_str ("");

everything is as expected.

More, if you try to get the length of this empty string. You get a compiler error in the first case, but OK in the second, (getting length 0)

Compiler error: "strings.cpp: In function ‘int main()’:
strings.cpp:10: error: request for member ‘empty’ in ‘empty_str’, which is of non-class type ‘std::string()’"

Weird, isn't it?. Do some of you have an explanation on this?, Am I an asshole and there is something obvious I haven't seen?

I'm using Linux Ubuntu. g++ gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)

Thank you and regards.
Fernando
closed account (z05DSL3A)
Use string empty_str; instead of string empty_str (); . The compiler sees string empty_str (); as a declareation of a function.
Last edited on
OOOps!

I'm an asshole.
Thank you very much, Grey wolf.

Regards.
closed account (z05DSL3A)
fernando wrote:
I'm an asshole.

Don't be so hard on yourself, you need to make such mistakes to learn from.
Yeah, and the constructor/function declaration duality is one of the most confusing aspects of the C++ grammar.
Grey Wolf you're so wise!
Topic archived. No new replies allowed.