473,782 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unary operator- question

To write a unary operator- operation that returns the negative of a value is
it just as simple as subtracting the value from itself?

ie.

{
return (value - value);
}

Thanks
Jul 22 '05 #1
7 3121
"John J" <@> wrote in message
news:44******** *************** *******@news.te ranews.com
To write a unary operator- operation that returns the negative of a
value is it just as simple as subtracting the value from itself?

ie.

{
return (value - value);
}

Thanks

Does 5-5 equal -5?
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
Jul 22 '05 #2
I can see this is going to be fun!

OK then, 5 - 5 != -5

Is anyone capable of providing some advice on the syntax I should be looking
at? I'm guessing there must be some simple code that will return the
negative of a value using the unary operator -

Tks

Jul 22 '05 #3

John J wrote:
I can see this is going to be fun!

OK then, 5 - 5 != -5
If you really want to do it that way, value - (2 * value) would do the
trick, I guess.
Is anyone capable of providing some advice on the syntax I should be looking
at? I'm guessing there must be some simple code that will return the
negative of a value using the unary operator -


Honestly, I'm trying to decode what the hell you mean, and failing.
After all:

int two = 2;
int negative_two = -two;

That's what you asked for if I were to take the literal interpretation
of your question.

But if you're trying to write the negation operator for some class, the
answer is that it depends on what's in your class. I mean, for example:

class number
{
public:
int value;

number operator-()
{
number result;
/*** --> ***/ result.value = -value; /*** <-- ***/
return result;
}
};

Or perhaps this:

class number
{
public:
bool negative;
unsigned int value;

number operator-()
{
number result;
result.value = value;
/*** --> ***/ result.negative = !negative; /*** <-- ***/
return result;
}
};

So I ask - what are you talking about?

mark

Jul 22 '05 #4
On Sun, 18 Apr 2004 03:07:11 GMT, "John J" <...@...> wrote:
I can see this is going to be fun!

OK then, 5 - 5 != -5

Is anyone capable of providing some advice on the syntax I should be looking
at? I'm guessing there must be some simple code that will return the
negative of a value using the unary operator -


I'm sure that a lot of people here are capable of writing your code,
and I'll bet that you are, too.

My advice is to step away from the computer, grab a piece of paper and
a pencil, and figure out how you would do it without the use of a
computer. Once you figure out the method, or algorithm, I'm sure that
you will see that writing the actual code is easy.

The most important thing in programming is not the hardware, or the
compiler, or even the language. The most important thing is that gray
squishy thing between your ears.
Jul 22 '05 #5
On Sun, 18 Apr 2004 03:07:11 GMT in comp.lang.c++, "John J" <...@...>
wrote,
Is anyone capable of providing some advice on the syntax I should be looking
at? I'm guessing there must be some simple code that will return the
negative of a value using the unary operator -


"unary" means only one operand.

Jul 22 '05 #6
John J wrote:
I can see this is going to be fun!

OK then, 5 - 5 != -5

Is anyone capable of providing some advice on the syntax I should be
looking at?
That depends on your class. Try to find out what it means to an object
of your class to be negated.
I'm guessing there must be some simple code that will return the
negative of a value using the unary operator -


Again, that depends on the meaning of your value. There is no universal
code for it. If you have a binary operator-, it might (or might not) be
possible to write the unary one as returning 0 - thevalue.

Jul 22 '05 #7
John J writes:
To write a unary operator- operation that returns the negative of a value is it just as simple as subtracting the value from itself?

ie.

{
return (value - value);
}


You need a language maven to answer that question. My best guess is that
the post you made is a binary operator. But I don't feel like traipsing
thought the BNF to try and arrive at an official answer. I have the
unsettling feeling I would have a good chance of getting it wrong anyhow,
after all that effort. If you don't get a suitable answer here, try posting
to one of the groups that have "standard" coded somehow in the name of the
group.
Jul 22 '05 #8

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

Similar topics

3
3909
by: Carlos Ribeiro | last post by:
I was checking the Prolog recipe in the Cookbook: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303057 It's a clever implementation that explores some aspects of Python that I wasn't aware of. One of them is the unary plus operator, that calls the __pos__ method. It's something that can be highly useful in my own experiments with the use of classes as a tool for generic declarative descriptions of objects -- UI forms,...
2
1884
by: Dave Theese | last post by:
Am I correct in saying that it is not possible to overload unary + and unary -?
8
3826
by: Elementary Penguin | last post by:
Is there a left add unary operator ( or do I have to overload one ) ? For example: string s1 = "A"; s1 += "B"; Console.WriteLine( s1 )
17
3502
by: Anoob | last post by:
Can we consider () unary operator when calling a function, in exps eq. f(), ( 1 + 2). But when we call function f( 10 ) it is a binary operator. Even if we pass f( 10, 20) as we are using , operator there one operand will be there for ()? And Unary operators like !, +, -, ~, ... are said to be having associativity right to left which means that we can write, (but not allowed) 1. 2! 2. !2
2
2372
by: Javier Estrada | last post by:
1. For types smaller than int, when I compile: class MyClass { static void Main(string args) { x = 10; y = -x; }
13
5100
by: Marc | last post by:
Hi, I've been lurking on clc for a few months now, and want to start by thanking the regulars here for opening my eyes to a whole new dimension of "knowing c". Considering I had never even touched the standards a year ago, though I graduated in embedded SW development... Anyway, to the problem at hand: I've stumbled upon the following construct at work recently, and cannot really make up my mind about the standard's take on the matter.
6
2590
by: Matthew Cook | last post by:
I would like to overload the unary minus operator so that I can negate an instance of a class and pass that instance to a function without creating an explicit temporary variable. Here is an example: #include <iostream> using namespace std; class Object { public:
28
5085
by: dspfun | last post by:
I'm trying to get a good understanding of how unary operators work and have some questions about the following test snippets. int *p; ~!&*++p--; It doesn't compile, why? The problem seems to be the ++, the compiler says: "Error: invalid l-value in increment". int i = 10; ~!*&i++;
16
4719
by: JoseMariaSola | last post by:
How may operators and operands does (typename) expression has? I'd say one operator, the cast operator, and two operands: typename and expression. But everywhere I read, cast is categorized as an unary operator. Why is that? Is it just a syntax cotegory? Thanks.
15
2017
by: =?Utf-8?B?VkIgRGV2ZWxvcGVy?= | last post by:
Overloading of the unary ++ operator in vb.net is not working. It show error: Operator declaration must be one of: +, -, *, \, /, ^, &, Like, Mod, And, Or, Xor, Not, <<, >>, =, <>, <, <=, >, >=, CType, IsTrue, IsFalse. Is there any way can to get rid of this error? C# is working fine for overloading ++, and do I need to change developing language to C#? Any suggestion?
0
10311
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6733
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
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
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.