operators

Conditional Operators in Javascript

Is it ok to use conditional operators like a statement like so? (x == y) ? alert("yo!") : alert("meh!"); Or is it more correct to use it to assign a value like so? z = (x == y) ? "yo!" : "meh!"; If it's not incorrect to use it like a statement, then is it possible to add more than one line of code for execution like so? Is it more ...

Cost of common operations for C#?

In Code Complete 2 (page 601 and 602) there is a table of "Cost of Common Operations". The baseline operation integer assignment is given a value 1 and then the relative time for common operations is listed for Java and C++. For example: C++ Java Integer assignment 1 1...

Canonical form of += operator for classes

I know that it's a good idea to make as much of the interface of a class non-member non-friend as possible, and I've just realised that for my 3D vector class, 'Vector3', I can move the +=, -= and so on operators out of the class, leaving just constructors and the copy assignment operator. The question is: what should this operator look...

What does the '%lt' mean in C++? (NOT modulus, I know what that does)

I once saw this line of code: std::cout %lt;%lt; "Hello world!" %lt;%lt; std:: endl; And am wondering what %lt;%lt; means. ...

C# implicit conversions and == operator

Some code for context: class a { } class b { public a a{get;set;} public static implicit operator a(b b) { return b.a; } } a a=null; b b=null; a = b; //compiler: cannot apply operator '==' to operands of type tralala... bool c = a == b; Is it possible to use == operator on different type instanc...

How do I write an overload operator where both arguments are interface

I'm using interface for most of my stuff. I can't find a way to create an overload operator + that would allow me to perform an addition on any objects implementing the IPoint interface Code interface IPoint { double X { get; set; } double Y { get; set; } } class Point : IPoint { double X { get; set; } double Y { ...

What is the * operator doing to this string in Ruby

Given the Ruby code line = "first_name=mickey;last_name=mouse;country=usa" record = Hash[*line.split(/=|;/)] I understand everything in the second line apart from the * operator - what is it doing and where is the documentation for this? (as you might guess, searching for this case is proving hard...) ...

Python: defining my own operators?

I have a bot in python that allows the users to evaluate mathematical expressions (via a set of safe functions), but I would like to define my own operator. Does python support such a thing? ...

What do these C operators mean?

I'm reading the book "Programming Challenges: The Programming Contest Training Manual" and are implementing a problem where I do not understand the use of operators c>>1 and the comparison if (n&1), someone could help me to know they mean? this is the example code #include <stdio.h> #define MAX_N 300 #define MAX_D 150 long cache[MAX_...

C# ?? operator in Ruby?

Is it possible to implement the ?? operator in Ruby? a = nil b = 1 x = a ?? b # x should == 1 x = b ?? 2 # x should == 1 ...

Binary operator overloading on a templated class (C++)

Hi all, I was recently trying to gauge my operator overloading/template abilities and as a small test, created the Container class below. While this code compiles fine and works correctly under MSVC 2008 (displays 11), both MinGW/GCC and Comeau choke on the operator+ overload. As I trust them more than MSVC, I'm trying to figure out what...

Better way to write this

Hi I just got hung up on this: if ( !(cmd === 'JustifyLeft' || cmd === 'JustifyRight' || cmd === 'JustifyCenter' || cmd === 'JustifyFull') ) (JavaScript) Any suggestions to do it better? thanks ...

Why does virtual assignment behave differently than other virtual functions of the same signature?

While playing with implementing a virtual assignment operator I have ended with a funny behavior. It is not a compiler glitch, since g++ 4.1, 4.3 and VS 2005 share the same behavior. Basically, the virtual operator= behaves differently than any other virtual function with respect to the code that is actually being executed. struct Base...

How to change default conjunction with Lucene MultiFieldQueryParser

I have some code using Lucene that leaves the default conjunction operator as OR, and I want to change it to AND. Some of the code just uses a plain QueryParser, and that's fine - I can just call setDefaultOperator on those instances. Unfortunately, in one place the code uses a MultiFieldQueryParser, and calls the static "parse" method...

Prevent misuse of logical operator instead of bitwise operators

In C++ it's possible to use a logical operator where a biwise operator was intended: int unmasked = getUnmasked(); //some wide value int masked = unmasked & 0xFF; // izolate lowest 8 bits the second statement could be easily mistyped: int masked = unmasked && 0xFF; //&& used instead of & This will cause incorrect behaviour - masked...

SQL Server *= Operator?

Today while inside a client's production system, I found a SQL Server query that contained an unfamiliar syntax. In the below example, what does the *= operator do? I could not find any mention of it on MSDN. The query does execute and return data. As far as anyone knows, this has been in the system since they were using SQL Server 2...

Pythonic way to implement three similar integer range operators?

Hello SO! I am working on a circular problem. In this problem, we have objects that are put on a ring of size MAX, and are assigned IDs from (0 to MAX-1). I have three simple functions to test for range inclusions. inRange(i,j,k) tests if i is in the circular interval [j,k[ (Mnemonic is i inRange(j,k)). And I have the same for ranges ]...

bitwise operator variations in C++

I read in a book that C++ provides additional operators to the usual &,|, and ! which are "and","or" and "not" respectively, plus they come with automatic short circuiting properties where applicable. I would like to use these operators in my code but for some reason the compiler interprets them as identifiers whenever i use them and thr...

How to make an "operator" variable? (C++)

I am working on making an expression class: template<typename T, typename U> class expression { public: expression(T vala, U valb, oper o){val1 = vala; val2 = valb; op = o;} operator bool{return(val1 op val2);} private: T val1; U val2; oper op; }; as you can see, this is somewhat pseudocode, because I need an opera...

How do you remember the LESS THAN and GREATER THAN operators?

I have been programming for about 5 years now, and still can't seem to understand, nor remember, what is less than, and what is greater than (< and >, respectively). I tend to debug twice every time I need to use this in a conditional, because I almost never get this right. Any guides to remembering and learning this? ...