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

J

john

Hi, in TC++PL3 on page 665, regarding valarray member functions, it is
mentioned:


"valarray operator-() const; // result= -v 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::eek:perator+

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

valarray<Type> operator+( ) 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<int> vaL ( 10 );
valarray<int> vaPLUS ( 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>"
 
J

Jim Langston

john said:
Hi, in TC++PL3 on page 665, regarding valarray member functions, it is
mentioned:


"valarray operator-() const; // result= -v 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::eek:perator+

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

valarray<Type> operator+( ) 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<int> vaL ( 10 );
valarray<int> vaPLUS ( 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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top