Re: Debugging error c2059
On Mar 3, 6:03 am, pauldepst...@att.net wrote:
On Mar 3, 12:33 pm, Ian Collins <ian-n...@hotmail.com> wrote:>
Correct code:
#include <string>
struct MyStruct
{
MyStruct(const std::string& SomeString, const std::string&
AnotherString)
{
}
};
MyStruct("SomeWord", "AnotherWord");
The posted code (with <string>) is fine.
Are you sure that it's ok not to assign a name to the
MyStruct("SomeWord", "AnotherWord") variable?
Sure, as long as the expression is in a function. (You can't
use an expression statement at global scope.) Put a function
definitioin around the last line, and it compiles fine.
Of course, the real question is what you want that line to do.
You said in your first posting that the code didn't do anything
useful, it was just an experiment. Which is definitely the case
if you don't name the variable---you construct a temporary of
type MyStruct, then immediately destroy it. This can be useful
if the temporary is part of a larger expression (and is used in
the larger expression), but it's very exceptional for it to be
useful as a complete expression (except maybe to test the
constructor of a stateless object in a unit test).
When I replaced that line by MyStruct
CallItSomething("SomeWord", "AnotherWord");
the bug appeared to be removed.
If you provide a name, the line is no longer an expression
statement, but a definition. Definitions can appear both at
namespace scope and at local scope. And the variable you create
with them will not be destructed until it goes out of scope.
--
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