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

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

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


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

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