468,771 Members | 1,565 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

What is the usage of "valarray operator+() const;"?

Hi, in TC++PL3 on page 665, regarding valarray member functions, it is
mentioned:
"valarray operator-() const; // result[i]= -v[i] for every element
// similarly: +, ~, !"

I checked the web and could not find anything that explains the need of
this "operator+".

Any ideas?

In MSDN it is mentioned:

"Standard C++ Library Reference
valarray::operator+

A unary operator that applies a plus to each element in a valarray.

valarray<Typeoperator+( ) const;

Return Value

A valarray whose elements are plus those of the operand array.
Example

// valarray_op_eplus.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
using namespace std;
int i;

valarray<intvaL ( 10 );
valarray<intvaPLUS ( 10 );
for ( i = 0 ; i < 10 ; i += 2 )
vaL [ i ] = -i;
for ( i = 1 ; i < 10 ; i += 2 )
vaL [ i ] = i-1;

cout << "The initial valarray is: ( ";
for ( i = 0 ; i < 10 ; i++ )
cout << vaL [ i ] << " ";
cout << ")." << endl;

vaPLUS = +vaL;
cout << "The element-by-element result of "
<< "the operator+ is the\n valarray: ( ";
for ( i = 0 ; i < 10 ; i++ )
cout << vaPLUS [ i ] << " ";
cout << ")." << endl;
}

Output

The initial valarray is: ( 0 0 -2 2 -4 4 -6 6 -8 8 ).
The element-by-element result of the operator+ is the
valarray: ( 0 0 -2 2 -4 4 -6 6 -8 8 ).

Requirements

Header: <valarray>"
Dec 15 '07 #1
2 1737
john wrote:
Hi, in TC++PL3 on page 665, regarding valarray member functions, it is
mentioned:
"valarray operator-() const; // result[i]= -v[i] for every element
// similarly: +, ~, !"

I checked the web and could not find anything that explains the need
of this "operator+".

Any ideas?

In MSDN it is mentioned:

"Standard C++ Library Reference
valarray::operator+

A unary operator that applies a plus to each element in a valarray.

valarray<Typeoperator+( ) const;

Return Value

A valarray whose elements are plus those of the operand array.
Example

// valarray_op_eplus.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
using namespace std;
int i;

valarray<intvaL ( 10 );
valarray<intvaPLUS ( 10 );
for ( i = 0 ; i < 10 ; i += 2 )
vaL [ i ] = -i;
for ( i = 1 ; i < 10 ; i += 2 )
vaL [ i ] = i-1;

cout << "The initial valarray is: ( ";
for ( i = 0 ; i < 10 ; i++ )
cout << vaL [ i ] << " ";
cout << ")." << endl;

vaPLUS = +vaL;
cout << "The element-by-element result of "
<< "the operator+ is the\n valarray: ( ";
for ( i = 0 ; i < 10 ; i++ )
cout << vaPLUS [ i ] << " ";
cout << ")." << endl;
}

Output

The initial valarray is: ( 0 0 -2 2 -4 4 -6 6 -8 8 ).
The element-by-element result of the operator+ is the
valarray: ( 0 0 -2 2 -4 4 -6 6 -8 8 ).
You're talking about the uniary + operator. The unary minus operator does
indeed make sense, returning the negative of a number or value, but, yes,
what sense does a unary + operator make since it would just return the
value? In most cases, it would indeed only return the value although you
could overload it to do anything you want (such as return the abs of the
value).

I think it's main purpose is for completeness, since someone can indeed do:

x = +y;


--
Jim Langston
ta*******@rocketmail.com
Dec 15 '07 #2
On Dec 15, 4:13 pm, john <j...@no.spamwrote:
Hi, in TC++PL3 on page 665, regarding valarray member
functions, it is mentioned:
"valarray operator-() const; // result[i]= -v[i] for every element
// similarly: +, ~, !"
I checked the web and could not find anything that explains
the need of this "operator+".
Any ideas?
What is the use of unary + on an int or a double? Logical (and
mathematical) coherence.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 16 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by fourfires.d | last post: by
5 posts views Thread by ano | last post: by
5 posts views Thread by ano | last post: by
8 posts views Thread by av | last post: by
2 posts views Thread by John Goche | last post: by
1 post views Thread by Sunny | last post: by
7 posts views Thread by Luna Moon | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.