473,396 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3107
"John J" <@> wrote in message
news:44******************************@news.teranew s.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
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...
2
by: Dave Theese | last post by:
Am I correct in saying that it is not possible to overload unary + and unary -?
8
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
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 ,...
2
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
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...
6
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...
28
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...
16
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...
15
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, <<, >>, =, <>, <, <=, >, >=,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.