Search Results

Search found 2508 results on 101 pages for 'ternary operator'.

Page 15/101 | < Previous Page | 11 12 13 14 15 16 17 18 19 20 21 22  | Next Page >

  • Concatenation Operator - PHP

    - by Chaitanya
    This might be a silly question but it struck me, and here i ask. <?php $x="Hi"; $y=" There"; $z = $x.$y; $a = "$x$y"; echo "$z"."<br />"."$a"; ?> $z uses the traditional concatenation operator provided by php and concatenates, conversely $a doesn't, My questions: a. by not using the concatenation operator, does it effect the performance? b. If it doesn't why at all have the concatenation operator. c. Why have 2 modes of implementation when one does the work?

    Read the article

  • Understanding pattern matching with cons operator

    - by Mathias
    In "Programming F#" I came across a pattern-matching like this one (I simplified a bit): let rec len list = match list with | [] -> 0 | [_] -> 1 | head :: tail -> 1 + len tail;; Practically, I understand that the last match recognizes the head and tail of the list. Conceptually, I don't get why it works. As far as I understand, :: is the cons operator, which appends a value in head position of a list, but it doesn't look to me like it is being used as an operator here. Should I understand this as a "special syntax" for lists, where :: is interpreted as an operator or a "match pattern" depending on context? Or can the same idea be extended for types other than lists, with other operators?

    Read the article

  • Should I make OR operator to return const reference or just reference

    - by Yan Cheng CHEOK
    class error_code { public: error_code() : hi(0), lo(0) {} error_code(__int64 lo) : hi(0), lo(lo) {} error_code(__int64 hi, __int64 lo) : hi(hi), lo(lo) {} error_code& operator|=(const error_code &e) { this->hi |= e.hi; this->lo |= e.lo; return *this; } __int64 hi; __int64 lo; }; error_code operator|(const error_code& e0, const error_code& e1) { return error_code(e0.hi | e1.hi, e0.lo | e1.lo); } int main() { error_code e0(1); error_code e1(2); e0 |= e1; } I was wondering, whether I should make operator|= to return a const error_code& or error_code& ?

    Read the article

  • Does dynamic_cast work inside overloaded operator delete ?

    - by iammilind
    I came across this: struct Base { void* operator new (size_t); void operator delete (void*); virtual ~Base () {} // <--- polymorphic }; struct Derived : Base {}; void Base::operator delete (void *p) { Base *pB = static_cast<Base*>(p); if(dynamic_cast<Derived*>(pB) != 0) { /* ... NOT reaching here ? ... */ } free(p); } Now if we do, Base *p = new Derived; delete p; Surprisingly, the condition inside the Base::delete is not satisfied Am I doing anything wrong ? Or casting from void* looses the information of Derived* ?

    Read the article

  • (C++) What's the difference between these overloaded operator functions?

    - by cv3000
    What is the difference between these two ways of overloading the != operator below. Which is consider better? Class Test { ...// private: int iTest public: BOOL operator==(const &Test test) const; BOOL operator!=(const &Test test) const; } BOOL operator==(const &Test test) const { return (iTest == test.iTest); } //overload function 1 BOOL Test::operator!=(const &Test test) const { return !operator==(test); } //overload function 2 BOOL Test::operator!=(const &Test test) const { return (iTest != test.iTest); } I've just recently seen function 1's syntax for calling a sibling operator function and wonder if writing it that way provides any benefits.

    Read the article

  • Why can't we have an immutable version of operator[] for map

    - by Yan Cheng CHEOK
    The following code works fine : std::map<int, int>& m = std::map<int, int>(); int i = m[0]; But not the following code : // error C2678: binary '[' : no operator... const std::map<int, int>& m = std::map<int, int>(); int i = m[0]; Most of the time, I prefer to make most of my stuff to become immutable, due to reason : http://www.javapractices.com/topic/TopicAction.do?Id=29 I look at map source code. It has mapped_type& operator[](const key_type& _Keyval) Is there any reason, why std::map unable to provide const mapped_type& operator[](const key_type& _Keyval) const

    Read the article

  • Is the "==" operator required to be defined to use std::find

    - by user144182
    Let's say I have: class myClass std::list<myClass> myList where myClass does not define the == operator and only consists of public fields. In both VS2010 and VS2005 the following does not compile: myClass myClassVal = myList.front(); std::find( myList.begin(), myList.end(), myClassVal ) complaining about lack of == operator. I naively assumed it would do a value comparison of the myClass object's public members, but I am almost positive this is not correct. I assume if I define a == operator or perhaps use a functor instead, it will solve the problem. Alternatively, if my list was holding pointers instead of values, the comparison would work. Is this right or should I be doing something else?

    Read the article

  • Entity Framework with SQL Server 2000 (APPLY Operator) issue

    - by How Lun
    Hello, I have a simple Linq query below: var seq = (from n in GetObjects() select n.SomeKey) .Distinct() .Count(); This query works find with SQL Server 2005 and above. But, this start to give headache when I hooked the EF to SQL Server 2000. Because EF is using APPLY operator which only SQL Server 2005 and above can be supported. I do not know why the hell EF is using APPLy operator instead of sub queries. My current work around is: var seq = (from n in GetObjects() select n.SomeKey) .Distinct() .ToList() .Count(); But, I can forsee more problems to come. The above query is just a simple one. Did anyone come across such issue? And how you guys work around it? Or is there a way to force EF not to use APPLY operator? Any help will be very much appreciated. How Lun.

    Read the article

  • When is #include <new> library required in C++?

    - by Czarak
    Hi, According to this reference entry for operator new ( http://www.cplusplus.com/reference/std/new/operator%20new/ ) : Global dynamic storage operator functions are special in the standard library: All three versions of operator new are declared in the global namespace, not in the std namespace. The first and second versions are implicitly declared in every translation unit of a C++ program: The header does not need to be included for them to be present. This seems to me to imply that the third version of operator new (placement new) is not implicitly declared in every translation unit of a C++ program and the header <new> does need to be included for it to be present. Is that correct? If so, how is it that using both g++ and MS VC++ Express compilers it seems I can compile code using the third version of new without #include <new> in my source code? Also, the MSDN Standard C++ Library reference entry on operator new gives some example code for the three forms of operator new which contains the #include <new> statement, however the example seems to compile and run just the same for me without this include? // new_op_new.cpp // compile with: /EHsc #include<new> #include<iostream> using namespace std; class MyClass { public: MyClass( ) { cout << "Construction MyClass." << this << endl; }; ~MyClass( ) { imember = 0; cout << "Destructing MyClass." << this << endl; }; int imember; }; int main( ) { // The first form of new delete MyClass* fPtr = new MyClass; delete fPtr; // The second form of new delete char x[sizeof( MyClass )]; MyClass* fPtr2 = new( &x[0] ) MyClass; fPtr2 -> ~MyClass(); cout << "The address of x[0] is : " << ( void* )&x[0] << endl; // The third form of new delete MyClass* fPtr3 = new( nothrow ) MyClass; delete fPtr3; } Could anyone shed some light on this and when and why you might need to #include <new> - maybe some example code that will not compile without #include <new> ? Thanks.

    Read the article

  • C++ addition overload ambiguity

    - by Nate
    I am coming up against a vexing conundrum in my code base. I can't quite tell why my code generates this error, but (for example) std::string does not. class String { public: String(const char*str); friend String operator+ ( const String& lval, const char *rval ); friend String operator+ ( const char *lval, const String& rval ); String operator+ ( const String& rval ); }; The implementation of these is easy enough to imagine on your own. My driver program contains the following: String result, lval("left side "), rval("of string"); char lv[] = "right side ", rv[] = "of string"; result = lv + rval; printf(result); result = (lval + rv); printf(result); Which generates the following error in gcc 4.1.2: driver.cpp:25: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: String.h:22: note: candidate 1: String operator+(const String&, const char*) String.h:24: note: candidate 2: String String::operator+(const String&) So far so good, right? Sadly, my String(const char *str) constructor is so handy to have as an implicit constructor, that using the explicit keyword to solve this would just cause a different pile of problems. Moreover... std::string doesn't have to resort to this, and I can't figure out why. For example, in basic_string.h, they are declared as follows: template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT, _Traits, _Alloc> operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs) template<typename _CharT, typename _Traits, typename _Alloc> basic_string<_CharT,_Traits,_Alloc> operator+(const _CharT* __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs); and so on. The basic_string constructor is not declared explicit. How does this not cause the same error I'm getting, and how can I achieve the same behavior??

    Read the article

  • why there is no power operator in java / c ++?

    - by RanZilber
    While there is such operator - ** in Python , i was wondering why java and c++ havent got one too. It is easy to make one for classes you define in C++ with operator overloading ( and i believe such thing is possible also in java) , but when talking about primitive types such as int, double and so on , you'll have to use library function like Math.power (and usaully have to cast both to double). So - why not define such operator for primitive types ?

    Read the article

  • Trying to reduce the speed overhead of an almost-but-not-quite-int number class

    - by Fumiyo Eda
    I have implemented a C++ class which behaves very similarly to the standard int type. The difference is that it has an additional concept of "epsilon" which represents some tiny value that is much less than 1, but greater than 0. One way to think of it is as a very wide fixed point number with 32 MSBs (the integer parts), 32 LSBs (the epsilon parts) and a huge sea of zeros in between. The following class works, but introduces a ~2x speed penalty in the overall program. (The program includes code that has nothing to do with this class, so the actual speed penalty of this class is probably much greater than 2x.) I can't paste the code that is using this class, but I can say the following: +, -, +=, <, > and >= are the only heavily used operators. Use of setEpsilon() and getInt() is extremely rare. * is also rare, and does not even need to consider the epsilon values at all. Here is the class: #include <limits> struct int32Uepsilon { typedef int32Uepsilon Self; int32Uepsilon () { _value = 0; _eps = 0; } int32Uepsilon (const int &i) { _value = i; _eps = 0; } void setEpsilon() { _eps = 1; } Self operator+(const Self &rhs) const { Self result = *this; result._value += rhs._value; result._eps += rhs._eps; return result; } Self operator-(const Self &rhs) const { Self result = *this; result._value -= rhs._value; result._eps -= rhs._eps; return result; } Self operator-( ) const { Self result = *this; result._value = -result._value; result._eps = -result._eps; return result; } Self operator*(const Self &rhs) const { return this->getInt() * rhs.getInt(); } // XXX: discards epsilon bool operator<(const Self &rhs) const { return (_value < rhs._value) || (_value == rhs._value && _eps < rhs._eps); } bool operator>(const Self &rhs) const { return (_value > rhs._value) || (_value == rhs._value && _eps > rhs._eps); } bool operator>=(const Self &rhs) const { return (_value >= rhs._value) || (_value == rhs._value && _eps >= rhs._eps); } Self &operator+=(const Self &rhs) { this->_value += rhs._value; this->_eps += rhs._eps; return *this; } Self &operator-=(const Self &rhs) { this->_value -= rhs._value; this->_eps -= rhs._eps; return *this; } int getInt() const { return(_value); } private: int _value; int _eps; }; namespace std { template<> struct numeric_limits<int32Uepsilon> { static const bool is_signed = true; static int max() { return 2147483647; } } }; The code above works, but it is quite slow. Does anyone have any ideas on how to improve performance? There are a few hints/details I can give that might be helpful: 32 bits are definitely insufficient to hold both _value and _eps. In practice, up to 24 ~ 28 bits of _value are used and up to 20 bits of _eps are used. I could not measure a significant performance difference between using int32_t and int64_t, so memory overhead itself is probably not the problem here. Saturating addition/subtraction on _eps would be cool, but isn't really necessary. Note that the signs of _value and _eps are not necessarily the same! This broke my first attempt at speeding this class up. Inline assembly is no problem, so long as it works with GCC on a Core i7 system running Linux!

    Read the article

  • Perl ||= operator for PHP and Javascript

    - by zaf
    Just been re-introduced to the Perl '||=' operator from the classic Orcish Maneuver example: keys my %or_cache = @in; @out = sort { ($or_cache{$a} ||= KEY($a)) cmp ($or_cache{$b} ||= KEY($b)) } @in; Is this operator available in PHP and Javascript? And if not, do these two languages allow user defined operators?

    Read the article

  • undefined C/C++ symbol as operator

    - by uray
    I notice that the character/symbol '`' and '@' is not used as an operator in C/C++, does anyone know the reason or historically why its so? if its really not used, is it safe to define those symbols as another operator/statement using #define?

    Read the article

  • Javascript IN operator compatibility

    - by jAndy
    Hi Folks, Can someone tell me since which ECMA version the IN operator is available and which browsers (versions) support it ? Explanation: The IN-operator can be used like the following: var myObject = { Firstname: 'Foo', Lastname: 'Bar' }; if('Lastname' in myObject){ // Lastname is an attribute of myObject }

    Read the article

  • c++ global operator not playing well with template class

    - by John
    ok, i found some similar posts on stackoverflow, but I couldn't find any that pertained to my exact situation and I was confused with some of the answers given. Ok, so here is my problem: I have a template matrix class as follows: template <typename T, size_t ROWS, size_t COLS> class Matrix { public: template<typename, size_t, size_t> friend class Matrix; Matrix( T init = T() ) : _matrix(ROWS, vector<T>(COLS, init)) { /*for( int i = 0; i < ROWS; i++ ) { _matrix[i] = new vector<T>( COLS, init ); }*/ } Matrix<T, ROWS, COLS> & operator+=( const T & value ) { for( vector<T>::size_type i = 0; i < this->_matrix.size(); i++ ) { for( vector<T>::size_type j = 0; j < this->_matrix[i].size(); j++ ) { this->_matrix[i][j] += value; } } return *this; } private: vector< vector<T> > _matrix; }; and I have the following global function template: template<typename T, size_t ROWS, size_t COLS> Matrix<T, ROWS, COLS> operator+( const Matrix<T, ROWS, COLS> & lhs, const Matrix<T, ROWS, COLS> & rhs ) { Matrix<T, ROWS, COLS> returnValue = lhs; return returnValue += lhs; } To me, this seems to be right. However, when I try to compile the code, I get the following error (thrown from the operator+ function): binary '+=' : no operator found which takes a right-hand operand of type 'const matrix::Matrix<T,ROWS,COLS>' (or there is no acceptable conversion) I can't figure out what to make of this. Any help if greatly appreciated!

    Read the article

  • Why isn't the boost::shared_ptr -> operator inlined?

    - by Alan
    Since boost::shared_ptr could be called very frequently and simply returns a pointer, isn't the -> operator a good candidate for being inlined? T * operator-> () const // never throws { BOOST_ASSERT(px != 0); return px; } Would a good compiler automatically inline this anyway? Should I lose any sleep over this? :-)

    Read the article

  • What does the C# operator => mean?

    - by Mr. Mark
    Answers to a recent post (Any chances to imitate times() Ruby method in C#?) use the = operator in the usage examples. What does this operator do? I can't locate it in my C# book, and it is hard to search for symbols like this online. (I couldn't find it.)

    Read the article

  • Is return an operator or a function?

    - by eSKay
    This is too basic I think, but how do both of these work? return true; // 1 and return (true); // 2 Similar: sizeof, exit My guess: If return was a function, 1 would be erroneous. So, return should be a unary operator that can also take in brackets... pretty much like unary minus: -5 and -(5), both are okay. Is that what it is - a unary operator?

    Read the article

  • PHP: Object Oriented Programming -> Operator

    - by oman9589
    So I've been reading through the book PHP Solutions, Dynamic Web Design Made Easy by David Powers. I read through the short section on Object Oriented PHP, and I am having a hard time grasping the idea of the - operator. Can anyone try to give me a solid explanation on the - operator in OOP PHP? Example: $westcost = new DateTimeZone('America/Los_Angeles'); $now->setTimezone($westcoast); Also,a more general example: $someObject->propertyName Thanks

    Read the article

< Previous Page | 11 12 13 14 15 16 17 18 19 20 21 22  | Next Page >