473,325 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Overloading the Insertion Operator

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
}

Could I do that in C++ too? Or is there a different way to do it?

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_

Aug 27 '07 #1
3 4662
Alf P. Steinbach wrote:
* kv******@hotmail.com:
>I've been asked to overload the insertion operator. What exactly is
the insertion operator in C++

There is no insertion operator in C++, but, stretching the English
language, "<<" is used as an insertion operator for streams, and some
authors call that the C++ "insertion operator".

>, 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
}

As far as I know even C99 doesn't support operator overloading.
lcc-win32, a C compiler, supports operator overloading as an extension.
Aug 27 '07 #2
On 2007-08-27 07:32, Alf P. Steinbach wrote:
* kv******@hotmail.com:
>I've been asked to overload the insertion operator. What exactly is
the insertion operator in C++

There is no insertion operator in C++, but, stretching the English
language, "<<" is used as an insertion operator for streams, and some
authors call that the C++ "insertion operator".

>>, 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
}

As far as I know even C99 doesn't support operator overloading.

In C++ you can't overload on pointer types.
To clarify, at least one of the operands of the operator has to be a
user defined type, i.e. you cannot change or add the operators of
built-in types.

--
Erik Wikström
Aug 27 '07 #3
Thanks!

Aug 27 '07 #4

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

Similar topics

2
by: ceo | last post by:
Hi there, I'm trying to overload insertion (<<) and extraction (>>) operators and the program yields output I didn't expect. Could someone please clarify what's wrong with my program. Thanks,...
2
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...
8
by: jois.de.vivre | last post by:
Hi, I'm having some trouble overloading the << operator. I have the following, very simple code: #include <iostream> using namespace std; class test { private: int val;
20
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...
1
by: acheron05 | last post by:
Hi, I've been writing a program for another school assignment but I am having trouble working out how to overload the < and << operators. The program is designed to read data from a file, create...
5
by: raan | last post by:
What I am trying to achieve here is depicted in the small program below. // Wrapit.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <map> #include...
16
by: EM.Bateman | last post by:
Working on Visual Studio .Net I've implemented a class: #ifndef CONTRIBUTOR_H #define CONTRIBUTOR_H enum Gender {male=1, female, unk}; #include <iostream> #include <iomanip> #include...
2
by: davoxol | last post by:
Hi, I have a problem using the insertion overloading. I defined it this way: friend ostream& operator<< (ostream& os, Base & base); I have to get specific output depending on the type of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.