const parameters
Say I have this function
void Test(const std::string &s)
now I want to call it with a temporary string like this:
Test(std::string("Hi"));
that works, but this doesn''t work:
void Test(std::string &s)
with this line
Test(std::string("Hi"));
I understand that such a temporary object is always constant, but I would really like to pass that kind of temporary objects as a non-const parameter. So are there ways of creating non-const temporary objects?
My Site
No. You can only have a
Why do you want to be able to do it? A cast isn't worth it as you can create exactly the same code:
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[edited by - Lektrix on October 13, 2003 12:13:35 PM]
const
reference to a temporary, yet you could do a hack.Why do you want to be able to do it? A cast isn't worth it as you can create exactly the same code:
void Test(std::string& s);
...
std::string tmp = "Hi";
Test(tmp);
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[edited by - Lektrix on October 13, 2003 12:13:35 PM]
Use a const_cast:
It can remove const and volatile attributes.
quote:
Grammar
postfix-expression:
const_cast < type-id > ( expression )
It can remove const and volatile attributes.
quote:Original post by Lektrix
No. Temporaries areconst
, and thus you can only have aconst
reference to a temporary, yet you could do a hack.
Why do you want to be able to do it? A cast isn''t worth it as you can create exactly the same code:
void Test(std::string& s);
...
std::string tmp = "Hi";
Test(tmp);
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[edited by - Lektrix on October 13, 2003 11:58:28 AM]
There''s no problem with passing an object to a function in the conventional way, but the temporary object is so beatyful in some cases, like this:
TEXTURE(RBFILELE("texture.bmp"));
in this case, the file would be opened, then the texture is loaded from the stream, and then the file would be closed immediately after that.
With the other way, you suggested, I either have to add a new scope, to get the file closed, or it will be closed on the end of the scope, and that can take pretty long in some functions. (if it''s in the beginning of WinMain, it will not be closed much earlier than you quit the program.
quote:Original post by DukeAtreides076
Use a const_cast:quote:
Grammar
postfix-expression:
const_cast < type-id > ( expression )
It can remove const and volatile attributes.
maybe I can do this, but then am I telling the user that he is passing a constant, while it could change, so I don''t really like to do that
My Site
quote:Original post by quasar3d
I understand that such a temporary object is always constant
Its not constant, the problem is that C++ imposes a rule to prevent you from binding a non-const reference to a temporary.
If I remember correctly, they are rvalues, i.e. non-addressable.
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement