Re: non-const reference to temporary
"Chris Morley" <chris.morley@lineone.net> wrote in message
news:48a6864c$0$2925$fa0fcedb@news.zen.co.uk...
WriteEverythingToFile(File("AFileName.txt"), std::string("This stuff
goes into the file"));
g++ will tell me this is an error because I'm passing a non const
reference to a temporary (anonymous variable with no name).
No I wouldn't have though it was an error either. What is your File
constructor like?
Does this compile & run?
After reading other people's comments I tried it with gcc too because it
works fine on VC6 & VS2008. My example doesn't compile.
You can cast like blargg says but (if I had to and didn't use MS compilers)
I'd do it like this rather than embedded in the call...
void WriteEverythingToFile( const File& file, const std::string& text)
{
File& myfile = const_cast<File&>(file);
myfile.write(text);
}
or... use a pointer & ignore the warning the compiler gives about taking the
address of a temporary (thanks gcc, but I am a grown up).
void WriteEverythingToFile( File* file, const std::string& text)
{
file->write(text);
}
or.. you could make the write a const function too & make the members you
change mutable (or force the this to a (File*) with a const cast inside the
File function.
void write_const( const std::string& stuff ) const
{
File* nonconstthis = const_cast< File* >(this);
nonconstthis->buf += stuff;
}
Below does work with gcc.
I would tend to agree with yours & Dave Harris' sentiment about this error.
It wouldn't be too hard to fix, MS did it in VC6 (presumably apparently
non-standard) but still refuses to compile DH's long& example as it should.
Anyway, hope these workarounds help.
Chris
---- 8< -------- 8< -------- 8< -------- 8< -------- 8< -------- 8< ----
#include <string>
#include "stdio.h"
class File {
public:
//File( const char* fn ) : filename(fn) {}
File( const std::string& fn ) : filename(fn) {}
~File() { display(); }
void write( const std::string& stuff ) { buf += stuff; }
void write_const( const std::string& stuff ) const
{
File* nonconstthis = const_cast< File* >(this);
nonconstthis->buf += stuff;
}
void display();
private:
std::string filename;
std::string buf;
};
void File::display()
{
printf( "Filename: %s\n", filename.c_str() );
printf( "Contents:\n%s\n", buf.c_str() );
printf( "Display end...\n\n" );
}
void WriteEverythingToFile( const File& file, const std::string& text)
{
File& myfile = const_cast<File&>(file);
myfile.write(text);
}
void WETF_pointer( File* file, const std::string& text)
{
file->write(text);
}
void WETF_inclass( const File& file, const std::string& text)
{
file.write_const(text);
}
int main(int argc, char* argv[])
{
WriteEverythingToFile(File("reference.txt"), std::string("This stuff goes
into the file"));
WETF_pointer(&File("pointer.txt"), std::string("This pointer stuff goes
into the file"));
WETF_inclass(File("in_class.txt"), std::string("This stuff goes into the
file"));
File("afilename").write("this is some text");
return 0;
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]