Reputation: 5029
I am working on a class that has a method with the following signature:
operator const std::string & () const
It's documented as "cast to string operator". I am wondering how to effectively invoke it. Unfortunately the following expression:
std::string(foo)
produces this error:
some_test.cpp:13:41: error: no matching function for call to'
std::basic_string<char>::basic_string(Foo (&)(std::string))'
Considering that foo
is of type Foo
, declared and instantiated as Foo foo(std::string(filename))
being a begginner of C++, this leaves me a bit confused. Any hints on what this means?
Upvotes: 0
Views: 1694
Reputation: 148965
If you question was about the usage of operator const std::string & () const
, it is quite simple : this operator is used when you need to convert to a std::string
. Example :
Foo foo(filename);
std::string s = foo; // uses the declared operator const std::string & () const
Upvotes: 0
Reputation: 254501
foo
is of typeFoo
, declared and instantiated asFoo foo(std::string(filename))
That's a function declaration, interpreting filename
as the name of a function parameter, equivalent to
Foo foo(std::string filename);
A variable declaration would look like
Foo foo(filename);
or, if you needed an explicit conversion (which you probably don't here)
Foo foo{std::string(filename)}; // C++11 or later
Foo foo = Foo(std::string(filename)); // historic dialects
Upvotes: 1