By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,960 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,960 IT Pros & Developers. It's quick & easy.

Overloading the Insertion Operator

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
Thanks!

Aug 27 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.