Search Results

Search found 977 results on 40 pages for 'operators'.

Page 13/40 | < Previous Page | 9 10 11 12 13 14 15 16 17 18 19 20  | Next Page >

  • How to change the meaning of pointer access operator

    - by kumar_m_kiran
    Hi All, This may be very obvious question, pardon me if so. I have below code snippet out of my project, #include <stdio.h> class X { public: int i; X() : i(0) {}; }; int main(int argc,char *arv[]) { X *ptr = new X[10]; unsigned index = 5; cout<<ptr[index].i<<endl; return 0; } Question Can I change the meaning of the ptr[index] ? Because I need to return the value of ptr[a[index]] where a is an array for subindexing. I do not want to modify existing source code. Any new function added which can change the behavior is needed. Since the access to index operator is in too many places (536 to be precise) in my code, and has complex formulas inside the index subscript operator, I am not inclined to change the code in many locations. PS : 1. I tried operator overload and came to conclusion that it is not possible. 2. Also p[i] will be transformed into *(p+i). I cannot redefine the basic operator '+'. So just want to reconfirm my understanding and if there are any possible short-cuts to achieve. Else I need fix it by royal method of changing every line of code :) .

    Read the article

  • Why is overloading operator&() prohibited for classes stored in STL containers?

    - by sharptooth
    Suddenly in this article ("problem 2") I see a statement that C++ Standard prohibits using STL containers for storing elemants of class if that class has an overloaded operator&(). Having overloaded operator&() can indeed be problematic, but looks like a default "address-of" operator can be used easily through a set of dirty-looking casts that are used in boost::addressof() and are believed to be portable and standard-compilant. Why is having an overloaded operator&() prohibited for classes stored in STL containers while the boost::addressof() workaround exists?

    Read the article

  • Php plugin to replace '->' with '.' as the member access operator ? Or even better: alternative synt

    - by Gigi
    Present day usable solution: Note that if you use an ide or an advanced editor, you could make a code template, or record a macro that inserts '->' when you press Ctrl and '.' or something. Netbeans has macros, and I have recorded a macro for this, and I like it a lot :) (just click the red circle toolbar button (start record macro),then type -> into the editor (thats all the macro will do, insert the arrow into the editor), then click the gray square (stop record macro) and assign the 'Ctrl dot' shortcut to it, or whatever shortcut you like) The php plugin: The php plugin, would also have to have a different string concatenation operator than the dot. Maybe a double dot ? Yea... why not. All it has to do is set an activation tag so that it doesnt replace / interpreter '.' as '->' for old scripts and scripts that dont intent do use this. Something like this: <php+ $obj.i = 5 ?> (notice the modified '<?php' tag to '<?php+' ) This way it wouldnt break old code. (and you can just add the '<?php+' code template to your editor and then type 'php tab' (for netbeans) and it would insert '<?php+' ) With the alternative syntax method you could even have old and new syntax cohabitating on the same page like this (I am illustrating this to show the great compatibility of this method, not because you would want to do this): <?php+ $obj.i = 5; ?> <?php $obj->str = 'a' . 'b'; ?> You could change the tag to something more explanatory, in case somebody who doesnt know about the plugin reads the script and thinks its a syntax error <?php-dot.com $obj.i = 5; ?> This is easy because most editors have code templates, so its easy to assign a shortcut to it. And whoever doesnt want the dot replacement, doesnt have to use it. These are NOT ultimate solutions, they are ONLY examples to show that solutions exist, and that arguments against replacing '->' with '.' are only excuses. (Just admit you like the arrow, its ok : ) With this potential method, nobody who doesnt want to use it would have to use it, and it wouldnt break old code. And if other problems (ahem... excuses) arise, they could be fixed too. So who can, and who will do such a thing ?

    Read the article

  • Logical value of an assignment in C

    - by Andy Shulman
    while (curr_data[1] != (unsigned int)NULL && ((curr_ptr = (void*)curr_data[1]) || 1)) Two part question. What will (curr_ptr = (void*)curr_data[1]) evaluate to, logically. TRUE? Also, I know its rather hack-ish, but is the while statement legal C? I would have to go through great contortions to put the assignment elsewhere in the code, so I'd be really nice if I could leave it there, but if it's so egregious that it makes everyone's eyeballs burst into flames, I'll change it.

    Read the article

  • Why do I need an intermediate conversion to go from struct to decimal, but not struct to int?

    - by Jesse McGrew
    I have a struct like this, with an explicit conversion to float: struct TwFix32 { public static explicit operator float(TwFix32 x) { ... } } I can convert a TwFix32 to int with a single explicit cast: (int)fix32 But to convert it to decimal, I have to use two casts: (decimal)(float)fix32 There is no implicit conversion from float to either int or decimal. Why does the compiler let me omit the intermediate cast to float when I'm going to int, but not when I'm going to decimal?

    Read the article

  • C++ vector<T>::iterator operator +

    - by Tom
    Hi, Im holding an iterator that points to an element of a vector, and I would like to compare it to the next element of the vector. Here is what I have Class Point{ public: float x,y; } //Somewhere in my code I do this vector<Point> points = line.getPoints(); foo (points.begin(),points.end()); where foo is: void foo (Vector<Point>::iterator begin,Vector<Point>::iterator end) { std::Vector<Point>::iterator current = begin; for(;current!=end-1;++current) { std::Vector<Point>::iterator next = current + 1; //Compare between current and next. } } I thought that this would work, but current + 1 is not giving me the next element of the vector. I though operator+ was the way to go, but doesnt seem so. Is there a workaround on this? THanks

    Read the article

  • Using the AND and NOT Operator in Python

    - by NoahClark
    Here is my custom class that I have that represents a triangle. I'm trying to write code that checks to see if self.a, self.b, and self.c are greater than 0, which would mean that I have Angle, Angle, Angle. Below you will see the code that checks for A and B, however when I use just self.a != 0 then it works fine. I believe I'm not using & correctly. Any ideas? Here is how I am calling it: print myTri.detType() class Triangle: # Angle A To Angle C Connects Side F # Angle C to Angle B Connects Side D # Angle B to Angle A Connects Side E def __init__(self, a, b, c, d, e, f): self.a = a self.b = b self.c = c self.d = d self.e = e self.f = f def detType(self): #Triangle Type AAA if self.a != 0 & self.b != 0: return self.a #If self.a > 10: #return AAA #Triangle Type AAS #elif self.a = 0: #return AAS #Triangle Type ASA #Triangle Type SAS #Triangle Type SSS #else: #return unknown

    Read the article

  • Calling all the 3 functions while using or operator even after returning true as a result.

    - by Shantanu Gupta
    I am calling three functions in my code where i want to validate some of my fields. When I tries to work with the code given below. It checks only for first value until it gets false result. I want some thing like that if fisrt function returns true then it should also call next function and so on. What can be used instead of Or Operator to do this. if (IsFieldEmpty(ref txtFactoryName, true, "Required") || IsFieldEmpty(ref txtShortName, true, "Required") || IsFieldEmpty(ref cboGodown, true, "Required")) { }

    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

  • C++ [] array operator with multiple arguments?

    - by genesys
    Can I define in C++ an array operator that takes multiple arguments? I tried it like this: const T& operator[](const int i, const int j, const int k) const{ return m_cells[k*m_resSqr+j*m_res+i]; } T& operator[](const int i, const int j, const int k){ return m_cells[k*m_resSqr+j*m_res+i]; } But I'm getting this error: error C2804 binary operator '[' has too many parameters

    Read the article

  • C# XOR on two byte variables will not compile without a cast

    - by Ash
    Why does the following raise a compile time error: 'Cannot implicitly convert type 'int' to 'byte': byte a = 25; byte b = 60; byte c = a ^ b; This would make sense if I were using an arithmentic operator because the result of a + b could be larger than can be stored in a single byte. However applying this to the XOR operator is pointless. XOR here it a bitwise operation that can never overflow a byte. using a cast around both operands works: byte c = (byte)(a ^ b);

    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

  • What is operator<< <> in C++?

    - by Austin Hyde
    I have seen this in a few places, and to confirm I wasn't crazy, I looked for other examples. Apparently this can come in other flavors as well, eg operator+ <>. However, nothing I have seen anywhere mentions what it is, so I thought I'd ask. It's not the easiest thing to google operator<< <>( :-)

    Read the article

  • Simple question about operator ||

    - by Tristan
    HEllo, i try to do that in FlashBuilder (FlexProject) protected function btn_detail_view_clickHandler(event:MouseEvent):void { CurrentState="Statistiques" || "PartMarche"; } But it's not working, i guess this is not the right syntax but what's the right syntax ? Thanks

    Read the article

  • Please explain this php expression "!$variable"

    - by DogBot
    What does an exclamaton mark in front of a variable mean? And how is it being used in this piece of code? EDIT: From the answers so far I suspect that I also should mention that this code is in a function where one of the parameters is $mytype ....would this be a way of checking if $mytype was passed? - Thanks to all of the responders so far. $myclass = null; if ($mytype == null && ($PAGE->pagetype <> 'site-index' && $PAGE->pagetype <>'admin-index')) { return $myclass; } elseif ($mytype == null && ($PAGE->pagetype == 'site-index' || $PAGE->pagetype =='admin-index')) { $myclass = ' active_tree_node'; return $myclass; } elseif (!$mytype == null && ($PAGE->pagetype == 'site-index' || $PAGE->pagetype =='admin-index')) { return $myclass; }`

    Read the article

  • while(0=0) evaluates to false

    - by paque
    b=10; while(a=b) { b--; if(b==-10)break; } B goes from 10 to -10. In my world, the while-statement, a=b, should always be true (since the assigment always "goes well"). That is not the case. When the loop stops, b will have a value of 0. In my world, it should pass 0 and go all the way to -10, when the if-statement kicks in. Have I misunderstood something major? (Code tested in IE8 and Adobe Acrobat)

    Read the article

  • Logical xor operator in c++?

    - by RAC
    Is there such a thing? First time I encountered a practical need for it, but I don't see one listed in stroustrup. I intend to write: // Detect when exactly one of A,B is equal to five. return (A==5) ^^ (B==5); But there is no ^^ operator. Can I use bitwise ^ here and get the right answer (regardless of machine representation of true and false)? I never mix & and &&, or | and ||, so I hesitate to do that with ^ and ^^. I'd be more comfortable writing my own "bool XOR(bool,bool)" function instead.

    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

  • Using operator+ without leaking memory?

    - by xokmzxoo
    So the code in question is this: const String String::operator+ (const String& rhs) { String tmp; tmp.Set(this->mString); tmp.Append(rhs.mString); return tmp; } This of course places the String on the stack and it gets removed and returns garbage. And placing it on the heap would leak memory. So how should I do this?

    Read the article

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

    - by GRB
    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 I'm doing wrong. Here is the code: #include <iostream> using namespace std; template <typename T> class Container { friend Container<T> operator+ <> (Container<T>& lhs, Container<T>& rhs); public: void setobj(T ob); T getobj(); private: T obj; }; template <typename T> void Container<T>::setobj(T ob) { obj = ob; } template <typename T> T Container<T>::getobj() { return obj; } template <typename T> Container<T> operator+ <> (Container<T>& lhs, Container<T>& rhs) { Container<T> temp; temp.obj = lhs.obj + rhs.obj; return temp; } int main() { Container<int> a, b; a.setobj(5); b.setobj(6); Container<int> c = a + b; cout << c.getobj() << endl; return 0; } This is the error Comeau gives: Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2 Copyright 1988-2008 Comeau Computing. All rights reserved. MODE:strict errors C++ C++0x_extensions "ComeauTest.c", line 27: error: an explicit template argument list is not allowed on this declaration Container<T> operator+ <> (Container<T>& lhs, Container<T>& rhs) ^ 1 error detected in the compilation of "ComeauTest.c". I'm having a hard time trying to get Comeau/MingGW to play ball, so that's where I turn to you guys. It's been a long time since my brain has melted this much under the weight of C++ syntax, so I feel a little embarrassed ;). Thanks in advance. EDIT: Eliminated an (irrelevant) lvalue error listed in initial Comeau dump.

    Read the article

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