473,569 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::opera tor+

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

valarray<Typeop erator+( ) const;

Return Value

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

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

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

valarray<intvaL ( 10 );
valarray<intvaP LUS ( 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 1940
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::opera tor+

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

valarray<Typeop erator+( ) const;

Return Value

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

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

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

valarray<intvaL ( 10 );
valarray<intvaP LUS ( 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*******@rocke tmail.com
Dec 15 '07 #2
On Dec 15, 4:13 pm, john <j...@no.spamwr ote:
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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 16 '07 #3

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

Similar topics

34
6412
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment operator. Here's what I'm trying to do. I've defined class Complex as class Complex { friend ostream &operator<<( ostream &, Complex & ); public:...
11
2686
by: fourfires.d | last post by:
Dear All, I am new to c++ and when write below code that try to call copy constructor in "=" operator overloading, it can not compile. Can anyone point out for me the reason? thanks !! class AA {
5
6262
by: ano | last post by:
Hi, I have converted this VB code to C# but I got this error: "Operator '||' cannot be applied to operands of type 'int' and 'short'" Is VB allow to use Operator "Or" with 'int'? If yes, how to do this in c#? Thanks,
5
3127
by: ano | last post by:
Hi all, I try to convert VB codes to C#. I got this error "Operator '!'cannot be applied to operand of type 'long'". Any ideas? Why doesn't "Not SYNCHRONIZE" equivalent to "!SYNCHRONIZE"? Thanks, VB:
8
1874
by: av | last post by:
i have my little string class, now i want do define something like this but for doing it i have to write sstream& operator+(char* a, char* b){} but compiler say something like "operator+(char*, char*) must be a member function or have a parameter of class type"
2
2753
by: John Goche | last post by:
Hello, Could anyone please provide with some information on the C++ overloaded cast operator and in which circumstances this might be useful? I have consulted several references but found no information on the uses of this operator. Thanks, JG
1
1750
by: Sunny | last post by:
The way to overload operator << is : ostream& operator << (ostream& os, const Obj& obj); and this is a member function. My question is why do we need to provide a const reference of Obj as argument when it is a member function and its members accessible through this pointer ? Isnt cout << obj; equivalent to obj.operator<<(cout) ? Thanks
7
1919
by: Luna Moon | last post by:
Hi all, I just couldn't get myself clear about the usage of "const" in front of and/or behind variables, pointers, classes, objects and functions... It's too confusing... any good clear article/tutorial that can help me? Thanks a lot!
7
3037
by: Bill Davy | last post by:
I want to be able to write (const char*)v where v is an item of type Class::ToolTypeT where ToolTypeT is an enumeration and I've tried everything that looks sensible. There's an ugly solution, but surely this is possible? I could define an operator<< but for various reasons, I really want to convert to a 'const char*' (to embed into a...
0
7710
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7625
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7935
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8144
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5244
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3677
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2128
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1235
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
969
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.