473,804 Members | 3,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Insertion operator onto object

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.
Jul 22 '05 #1
2 1742
cpp
correction: the code line before the last should be
*myout << "This is a number" << 5;
Jul 22 '05 #2
"cpp" <cp*@on.the.net > wrote in message
news:uX******** ***********@twi ster.nyroc.rr.c om...
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:
The standard already provides such a class: ofstream. (Okay, a
typedef.)
-----------------------------------
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);
}
These constructors are dangerous -- how long will the const char* used
to initialize path remain valid? Store a std::string instead.

How do I define an operator function so that: *myout << "This is a

number";

See the recent thread "Subclassin g to get << and >>". Summary: it's a
waste of time to implement all the overloads of << -- that's what
standard streams are for.

Jonathan
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3830
by: ryan.fairchild | last post by:
I have a problem I am trying to create a MyInt class to hanlde very large ints. Its for a class, therefore I can only do what the teach tells me. I want to be able to overload the insertion operator so that I can read in one digit at a time from the buffer and if the digit is 0 - 9 then put it into the dynamic array which stores each digit of this large int. #include <iostream> #include <string> #include "myint.h"
8
2752
by: bipod.rafique | last post by:
Hello All, I need your help in understanding something. I have a simple class class test{ };
20
3873
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in a named namespace namespace foo { class Foo
6
3597
by: jack | last post by:
I have a class which overloads the insertion operator '<<' for every type that ostream handles. I do this so that I can use my class a direct replacement for cout and cerr where the insertion syntax is used. The reason for this is that I have a log file class that needs to know how many lines have been written. When the line exceeds a certain number, I need to close the log file, and open a new one. I would like to be able to track...
4
4503
by: Raider | last post by:
Is there std::map member-function that do as code below? typedef std::map<NameClass, ValueClass> ParameterContainer; .... // this code is equivalent to "_Parameters = Value", // but a bit faster for insertion <code>
2
7379
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of number that are input so it selects the values from the default constructor. Can someone assist me with the insertion function. The code is below. // Definition of class Complex #ifndef COMPLEX1_H
0
2061
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the increment operator in a class and then defining a derived class and using the derived class. When I try to use the increment operator on an instance of the derived class (as an instance of the derived class, not when used as an instance of the base...
5
2613
by: phiefer3 | last post by:
I'm currently a student, but this problem isn't directly related to what I have to do on an assignment. It's just a problem I've had with some supporting features. First of all, I'm using MSVS 2005 standard edition. I haven't downloaded or acquired any additional libraries or tools, just what gets installed with the studio by default. Also, I'm using C++ and started the problem projects as an empty CLR project. Currently I've had several...
3
4692
by: kvnsmnsn | last post by:
I've been asked to overload the insertion operator. What exactly is the insertion operator in C++, and how would one overload it? I think that in C I could overload the "+" operator like so: double operator+ ( char* left , char* right) { // some code here }
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5516
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2988
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.