Archived

This topic is now archived and is closed to further replies.

const parameters

This topic is 5174 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
No. You can only have a 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]

Share this post


Link to post
Share on other sites
quote:
Original post by Lektrix
No. Temporaries are const, and thus you can only have a 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 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites