Re: non-const reference to temporary
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?
Chris
---- 8< -------- 8< -------- 8< -------- 8< -------- 8< -------- 8< ----
#include <string>
#include "stdio.h"
#include "conio.h"
class File {
public:
// File( const char* fn ) : filename(fn) {} //ok too
File( const std::string& fn ) : filename(fn) {}
~File() { display(); }
void write( const std::string& stuff ) { 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( File& file, const std::string& text)
{
file.write(text);
}
int main(int argc, char* argv[])
{
WriteEverythingToFile(File("AFileName.txt"), std::string("This stuff goes
into the file"));
File("afilename").write("this is some text");
_getch();
return 0;
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]