C
cpp
Ok, I have been tearing my hair out trying to get this to work, so anything you
can do I will greatly appreciate. I have this small class which is supposed to
make, for its users, outputting to a file easy:
-----------------------------------
class Out {
private:
const char *path;
ofstream outfile;
public:
Out(const char *c);
Out(const string &s);
~Out();
void close();
};
Out::Out(const char *c)
{
path = c;
ofs.open(path);
}
Out::Out(const string &s)
{
path = s.c_str();
ofs.open(path);
}
Out::~Out()
{
delete path;
}
void Out::close()
{
ofs.close();
}
-----------------------------------
As you can see, the Out class has an ofstream member variable associated with
it. Now what I want to do is overload the insertion operator << so that I can
use it on an Out object, and have the effect of inserting into Out's ofstream
variable.
int main()
{
Out *myout = new Out("test.txt");
*myout << "This is a number" << 5;
myout->close();
return 0;
}
How do I define an operator function so that: *myout << "This is a number";
should do the following? : myout->outfile << "This is a number" << 5;
Thanks a million.
can do I will greatly appreciate. I have this small class which is supposed to
make, for its users, outputting to a file easy:
-----------------------------------
class Out {
private:
const char *path;
ofstream outfile;
public:
Out(const char *c);
Out(const string &s);
~Out();
void close();
};
Out::Out(const char *c)
{
path = c;
ofs.open(path);
}
Out::Out(const string &s)
{
path = s.c_str();
ofs.open(path);
}
Out::~Out()
{
delete path;
}
void Out::close()
{
ofs.close();
}
-----------------------------------
As you can see, the Out class has an ofstream member variable associated with
it. Now what I want to do is overload the insertion operator << so that I can
use it on an Out object, and have the effect of inserting into Out's ofstream
variable.
int main()
{
Out *myout = new Out("test.txt");
*myout << "This is a number" << 5;
myout->close();
return 0;
}
How do I define an operator function so that: *myout << "This is a number";
should do the following? : myout->outfile << "This is a number" << 5;
Thanks a million.