3 回答
There is very little relation between the two snippets.
The first one declares and initializes the variable lRoot
, nothing to see there.
The second snippet declares and default-initializes lRoot
in the first line, but then it goes on to invoke operator()
on lRoot
with an argument of type Ogre::root*
. Since std::auto_ptr
does not define an operator()
, the compiler produces the given error.
The first statement is a declaration of variable lRoot
with initialisation (using the syntax of initialiser in parentheses).
The second is a declaration of default-initialised variable lRoot
, followed by invoking operator()
on the variable. (Note that std::auto_ptr
doesn't define such an operator).
To split this into two lines (still as one statement), you can just insert a line break anywhere whitespace is allowed:
std::auto_ptr<Ogre::Root> lRoot(
new Ogre::Root(lConfigFileName, lPluginsFileName, lLogFileName));
To actually split this into a declaration and assignment (note that when split, it cannot be an initialisation), you could do this:
std::auto_ptr<Ogre::Root> lRoot;
lRoot.reset(new Ogre::Root(lConfigFileName, lPluginsFileName, lLogFileName));
They are not the same thing!
You are not just splitting the statement into two lines. The two are two different statements..
You can split the first statement onto two lines like this:
std::auto_ptr<Ogre::Root> lRoot
(new Ogre::Root(lConfigFileName, lPluginsFileName, lLogFileName));
And it would compile just fine because the multiple white spaces are ignored.