Search Results

Search found 3861 results on 155 pages for 'assignment operator'.

Page 7/155 | < Previous Page | 3 4 5 6 7 8 9 10 11 12 13 14  | Next Page >

  • perl - universal operator overload

    - by Todd Freed
    I have an idea for perl, and I'm trying to figure out the best way to implement it. The idea is to have new versions of every operator which consider the undefined value as the identity of that operation. For example: $a = undef + 5; # undef treated as 0, so $a = 5 $a = undef . "foo"; # undef treated as '', so $a = foo $a = undef && 1; # undef treated as false, $a = true and so forth. ideally, this would be in the language as a pragma, or something. use operators::awesome; However, I would be satisfied if I could implement this special logic myself, and then invoke it where needed: use My::Operators; The problem is that if I say "use overload" inside My::Operators only affects objects blessed into My::Operators. So the question is: is there a way (with "use overoad" or otherwise) to do a "universal operator overload" - which would be called for all operations, not just operations on blessed scalars. If not - who thinks this would be a great idea !? It would save me a TON of this kind of code if($object && $object{value} && $object{value} == 15) replace with if($object{value} == 15) ## the special "is-equal-to" operator

    Read the article

  • Recursion problem overloading an operator

    - by Tronfi
    I have this: typedef string domanin_name; And then, I try to overload the operator< in this way: bool operator<(const domain_name & left, const domain_name & right){ int pos_label_left = left.find_last_of('.'); int pos_label_right = right.find_last_of('.'); string label_left = left.substr(pos_label_left); string label_right = right.substr(pos_label_right); int last_pos_label_left=0, last_pos_label_right=0; while(pos_label_left!=string::npos && pos_label_right!=string::npos){ if(label_left<label_right) return true; else if(label_left>label_right) return false; else{ last_pos_label_left = pos_label_left; last_pos_label_right = pos_label_right; pos_label_left = left.find_last_of('.', last_pos_label_left); pos_label_right = right.find_last_of('.', last_pos_label_left); label_left = left.substr(pos_label_left, last_pos_label_left); label_right = right.substr(pos_label_right, last_pos_label_right); } } } I know it's a strange way to overload the operator <, but I have to do it this way. It should do what I want. That's not the point. The problem is that it enter in an infinite loop right in this line: if(label_left<label_right) return true; It seems like it's trying to use this overloading function itself to do the comparision, but label_left is a string, not a domain name! Any suggestion?

    Read the article

  • overload == (and != , of course) operator, can I bypass == to determine whether the object is null

    - by LLS
    Hello, when I try to overload operator == and != in C#, and override Equal as recommended, I found I have no way to distinguish a normal object and null. For example, I defined a class Complex. public static bool operator ==(Complex lhs, Complex rhs) { return lhs.Equals(rhs); } public static bool operator !=(Complex lhs, Complex rhs) { return !lhs.Equals(rhs); } public override bool Equals(object obj) { if (obj is Complex) { return (((Complex)obj).Real == this.Real && ((Complex)obj).Imaginary == this.Imaginary); } else { return false; } } But when I want to use if (temp == null) When temp is really null, some exception happens. And I can't use == to determine whether the lhs is null, which will cause infinite loop. What should I do in this situation. One way I can think of is to us some thing like Class.Equal(object, object) (if it exists) to bypass the == when I do the check. What is the normal way to solve the problem?

    Read the article

  • Operator Overloading << in c++

    - by thlgood
    I'm a fresh man in C++. I write this simple program to practice Overlaoding. This is my code: #include <iostream> #include <string> using namespace std; class sex_t { private: char __sex__; public: sex_t(char sex_v = 'M'):__sex__(sex_v) { if (sex_v != 'M' && sex_v != 'F') { cerr << "Sex type error!" << sex_v << endl; __sex__ = 'M'; } } const ostream& operator << (const ostream& stream) { if (__sex__ == 'M') cout << "Male"; else cout << "Female"; return stream; } }; int main(int argc, char *argv[]) { sex_t me('M'); cout << me << endl; return 0; } When I compiler it, It print a lots of error message: The error message was in a mess. It's too hard for me to found useful message sex.cpp: ???‘int main(int, char**)’?: sex.cpp:32:10: ??: ‘operator<<’?‘std::cout << me’????? sex.cpp:32:10: ??: ???: /usr/include/c++/4.6/ostream:110:7: ??: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostre

    Read the article

  • Why overload true and false instead of defining bool operator?

    - by Joe Enos
    I've been reading about overloading true and false in C#, and I think I understand the basic difference between this and defining a bool operator. The example I see around is something like: public static bool operator true(Foo foo) { return (foo.PropA > 0); } public static bool operator false(Foo foo) { return (foo.PropA <= 0); } To me, this is the same as saying: public static implicit operator bool(Foo foo) { return (foo.PropA > 0); } The difference, as far as I can tell, is that by defining true and false separately, you can have an object that is both true and false, or neither true nor false: public static bool operator true(Foo foo) { return true; } public static bool operator false(Foo foo) { return true; } //or public static bool operator true(Foo foo) { return false; } public static bool operator false(Foo foo) { return false; } I'm sure there's a reason this is allowed, but I just can't think of what it is. To me, if you want an object to be able to be converted to true or false, a single bool operator makes the most sense. Can anyone give me a scenario where it makes sense to do it the other way? Thanks

    Read the article

  • C++ OOP - Can you 'overload a cast' <- hard to explain in 1 sentence

    - by Brandon Miller
    Well, the WinAPI has a POINT struct, but I am trying to make an alternative class to this so you can set the values of x and y from a constructor. /** * X-Y coordinates */ class Point { public: int X, Y; Point(void) : X(0), Y(0) {} Point(int x, int y) : X(x), Y(y) {} Point(const POINT& pt) : X(pt.x), Y(pt.y) {} Point& operator= (const POINT& other) { X = other.x; Y = other.y; } }; // I have an assignment operator and copy constructor. Point myPtA(3,7); Point myPtB(8,5); POINT pt; pt.x = 9; pt.y = 2; // I can assign a 'POINT' to a 'Point' myPtA = pt; // But I also want to be able to assign a 'Point' to a 'POINT' pt = myPtB; Is it possible to overload operator= in a way so that I can assign a Point to a POINT? Or maybe some other method to achieve this? Thanks in advance.

    Read the article

  • operator for enums

    - by Veer
    Hi all, Just out of curiosity, asking this Like the expression one below a = (condition) ? x : y; // two outputs why can't we have an operator for enums? say, myValue = f ??? fnApple() : fnMango() : fnOrange(); // no. of outputs specified in the enum definition instead of switch statements (eventhough refractoring is possible) enum Fruit { apple, mango, orange }; Fruit f = Fruit.apple; Or is it some kind of useless operator?

    Read the article

  • Operator overloading outside class

    - by bobobobo
    There are two ways to overload operators for a C++ class: Inside class class Vector2 { public: float x, y ; Vector2 operator+( const Vector2 & other ) { Vector2 ans ; ans.x = x + other.x ; ans.y = y + other.y ; return ans ; } } ; Outside class class Vector2 { public: float x, y ; } ; Vector2 operator+( const Vector2& v1, const Vector2& v2 ) { Vector2 ans ; ans.x = v1.x + v2.x ; ans.y = v1.y + v2.y ; return ans ; } (Apparently in C# you can only use the "outside class" method.) In C++, which way is more correct? Which is preferable?

    Read the article

  • Java operator overload

    - by rengolin
    Coming from C++ to Java, the obvious unanswered question is why not operator overload. On the web some go about: "it's clearly obfuscated and complicate maintenance" but no one really elaborates that further (I completely disagree, actually). Other people pointed out that some objects do have an overload (like String operator +) but that is not extended to other objects nor is extensible to the programmer's decision. I've heard that they're considering extending the favour to BigInt and similar, but why not open that for our decisions? How exactly if complicates maintenance and where on earth does this obfuscate code? Isn't : Complex a, b, c; a = b + c; much simpler than: Complex a, b, c; a.equals( b.add(c) ); ??? Or is it just habit?

    Read the article

  • null coalescing operator for javascript?

    - by Daniel Schaffer
    I assumed this question has already been asked here, but I couldn't find any, so here it goes: Is there a null coalescing operator in Javascript? For example, in C#, I can do this: String someString = null; var whatIWant = someString ?? "Cookies!"; The best approximation I can figure out for Javascript is using the conditional operator: var someString = null; var whatIWant = someString ? someString : 'Cookies!'; Which is sorta icky IMHO. Can I do better?

    Read the article

  • Operator overloading C++ outside class

    - by bobobobo
    Well, so there are 2 ways to overload operators for a C++ class INSIDE CLASS class Vector2 { public: float x, y ; Vector2 operator+( const Vector2 & other ) { Vector2 ans ; ans.x = x + other.x ; ans.y = y + other.y ; return ans ; } } ; OUTSIDE CLASS class Vector2 { public: float x, y ; } ; Vector2 operator+( const Vector2& v1, const Vector2& v2 ) { Vector2 ans ; ans.x = v1.x + v2.x ; ans.y = v1.y + v2.y ; return ans ; } In C# apparently you can only use the OUTSIDE class method The question is, in C++, which is "morer-correcter?" Which is preferable? When is one way better than another?

    Read the article

  • no match for operator= using a std::vector

    - by Max
    I've got a class declared like this: class Level { private: std::vector<mapObject::MapObject> features; (...) }; and in one of its member functions I try to iterate through that vector like this: vector<mapObject::MapObject::iterator it; for(it=features.begin(); it<features.end(); it++) { /* loop code */ } This seems straightforward to me, but g++ gives me this error: src/Level.cpp:402: error: no match for ‘operator=’ in ‘it = ((const yarl::level::Level*)this)-yarl::level::Level::features.std::vector<_Tp, _Alloc::begin [with _Tp = yarl::mapObject::MapObject, _Alloc = std::allocator<yarl::mapObject::MapObject>]()’ /usr/include/c++/4.4/bits/stl_iterator.h:669: note: candidates are: __gnu_cxx::__normal_iterator<yarl::mapObject::MapObject*,std::vector & __gnu_cxx::__normal_iterator<yarl::mapObject::MapObject*,std::vector >::operator=(const __gnu_cxx::__normal_iterator<yarl::mapObject::MapObject*, ``std::vector<yarl::mapObject::MapObject, std::allocator<yarl::mapObject::MapObject> > >&) Anyone know why this is happening?

    Read the article

  • Problem with operator ==

    - by CPPDev
    I am facing some problem with use of operator == in the following c++ program. #include < iostream> using namespace std; class A { public: A(char *b) { a = b; } A(A &c) { a = c.a; } bool operator ==(A &other) { return strcmp(a, other.a); } private: char *a; }; int main() { A obj("test"); A obj1("test1"); if(obj1 == A("test1")) { cout<<"This is true"<<endl; } } What's wrong with if(obj1 == A("test1")) line ?? Any help is appreciated.

    Read the article

  • C++ overloading operator comma for variadic arguments

    - by uray
    is it possible to construct variadic arguments for function by overloading operator comma of the argument? i want to see an example how to do so.., maybe something like this: template <typename T> class ArgList { public: ArgList(const T& a); ArgList<T>& operator,(const T& a,const T& b); } //declaration void myFunction(ArgList<int> list); //in use: myFunction(1,2,3,4); //or maybe: myFunction(ArgList<int>(1),2,3,4);

    Read the article

  • C# String Operator Overloading

    - by ScottSEA
    G'Day Mates - What is the right way (excluding the argument of whether it is advisable) to overload the string operators <, , <= and = ? I've tried it five ways to Sunday and I get various errors - my best shot was declaring a partial class and overloading from there, but it won't work for some reason. namespace System { public partial class String { public static Boolean operator <(String a, String b) { return a.CompareTo(b) < 0; } public static Boolean operator >(String a, String b) { return a.CompareTo(b) > 0; } } }

    Read the article

  • An operator == whose parameters are non-const references

    - by Eduardo León
    I this post, I've seen this: class MonitorObjectString: public MonitorObject { // some other declarations friend inline bool operator==(/*const*/ MonitorObjectString& lhs, /*const*/ MonitorObjectString& rhs) { return lhs.fVal==rhs.fVal; } } Before we can continue, THIS IS VERY IMPORTANT: I am not questioning anyone's ability to code. I am just wondering why someone would need non-const references in a comparison. The poster of that question did not write that code. This was just in case. This is important too: I added both /*const*/s and reformatted the code. Now, we get back to the topic: I can't think of a sane use of the equality operator that lets you modify its by-ref arguments. Do you?

    Read the article

  • Dynamic Operator Overloading on dict classes in Python

    - by Ishpeck
    I have a class that dynamically overloads basic arithmetic operators like so... import operator class IshyNum: def __init__(self, n): self.num=n self.buildArith() def arithmetic(self, other, o): return o(self.num, other) def buildArith(self): map(lambda o: setattr(self, "__%s__"%o,lambda f: self.arithmetic(f, getattr(operator, o))), ["add", "sub", "mul", "div"]) if __name__=="__main__": number=IshyNum(5) print number+5 print number/2 print number*3 print number-3 But if I change the class to inherit from the dictionary (class IshyNum(dict):) it doesn't work. I need to explicitly def __add__(self, other) or whatever in order for this to work. Why?

    Read the article

  • Scala :: operator, how it works?

    - by Felix
    Hello Guys, in Scala, I can make a caseclass case class Foo(x:Int) and then put it in a list like so: List(Foo(42)) Now, nothing strange here. The following is strange to me. The operator :: is a function on a list, right? With any function with 1 argument in Scala, I can call it with infix notation. An example is 1 + 2 is a function (+) on the object Int. The class Foo I just defined does not have the :: operator, so how is the following possible: Foo(40) :: List(Foo(2)) ? In scala 2.8 rc1, I get the following output from the interactive prompt: scala> case class Foo(x:Int) defined class Foo scala> Foo(40) :: List(Foo(2)) res2: List[Foo] = List(Foo(40), Foo(2)) scala> I can go on and use it, but if someone can explain it I will be glad :)

    Read the article

  • Passing operator as a parameter

    - by nacho4d
    Hi, I want to have a function that evaluates 2 bool vars (like a truth table) for example: since T | F : T then myfunc('t', 'f', ||); /*defined as: bool myfunc(char lv, char rv, ????)*/ should return true; how can I pass the third parameter? (I know is possible to pass it as a char* but then I will have to have another table to compare operator string and then do the operation which is something I would like to avoid) Is it possible to pass an operator like ^(XOR) or ||(OR) or &&(AND), etc in a function/method? Thanks in advance

    Read the article

  • copy C'tor with operator= | C++

    - by user2266935
    I've got this code here: class DerivedClass : public BaseClass { SomeClass* a1; Someclass* a2; public: //constructors go here ~DerivedClass() { delete a1; delete a2;} // other functions go here .... }; My first question is as follows: Can I write an "operator=" to "DerivedClass" ? (if your answer is yes, could you show me how?) My second question is: If the answer to the above is yes, could you show me how to make an "copy c'tor" using the "operator=" that you wrote beforehand (if that is even possible)? Your help would be much appreciated !

    Read the article

  • template; operator (int)

    - by Oops
    Hi, regarding my Point struct already mentioned here: http://stackoverflow.com/questions/2794369/template-class-ctor-against-function-new-c-standard is there a chance to replace the function toint() with a cast-operator (int)? namespace point { template < unsigned int dims, typename T > struct Point { T X[ dims ]; //umm??? template < typename U > Point< dims, U > operator U() const { Point< dims, U > ret; std::copy( X, X + dims, ret.X ); return ret; } //umm??? Point< dims, int > operator int() const { Point<dims, int> ret; std::copy( X, X + dims, ret.X ); return ret; } //OK Point<dims, int> toint() { Point<dims, int> ret; std::copy( X, X + dims, ret.X ); return ret; } }; //struct Point template < typename T > Point< 2, T > Create( T X0, T X1 ) { Point< 2, T > ret; ret.X[ 0 ] = X0; ret.X[ 1 ] = X1; return ret; } }; //namespace point int main(void) { using namespace point; Point< 2, double > p2d = point::Create( 12.3, 34.5 ); Point< 2, int > p2i = (int)p2d; //äähhm??? std::cout << p2d.str() << std::endl; char c; std::cin >> c; return 0; } I think the problem is here that C++ cannot distinguish between different return types? many thanks in advance. regards Oops

    Read the article

  • null-coalescing operator or conditional operator

    - by rkrauter
    Which coding style do you prefer: object o = new object(); //string s1 = o ?? "Tom"; // Cannot implicitly convert type 'object' to 'string' CS0266 string s3 = Convert.ToString(o ?? "Tom"); string s2 = (o != null) ? o.ToString() : "Tom"; s2 or s3? Is it possible to make it shorter? s1 does not obviously work.

    Read the article

  • Working with Reporting Services Filters – Part 2: The LIKE Operator

    - by smisner
    In the first post of this series, I introduced the use of filters within the report rather than in the query. I included a list of filter operators, and then focused on the use of the IN operator. As I mentioned in the previous post, the use of some of these operators is not obvious, so I'm going to spend some time explaining them as well as describing ways that you can use report filters in Reporting Services in this series of blog posts. Now let's look at the LIKE operator. If you write T-SQL queries, you've undoubtedly used the LIKE operator to produce a query using the % symbol as a wildcard for multiple characters like this: select * from DimProduct where EnglishProductName like '%Silver%' And you know that you can use the _ symbol as a wildcard for a single character like this: select * from DimProduct where EnglishProductName like '_L Mountain Frame - Black, 4_'   So when you encounter the LIKE operator in a Reporting Services filter, you probably expect it to work the same way. But it doesn't. You use the * symbol as a wildcard for multiple characters as shown here: Expression Data Type Operator Value [EnglishProductName] Text Like *Silver* Note that you don’t have to include quotes around the string that you use for comparison. Books Online has an example of using the % symbol as a wildcard for a single character, but I have not been able to successfully use this wildcard. If anyone has a working example, I’d love to see it!

    Read the article

< Previous Page | 3 4 5 6 7 8 9 10 11 12 13 14  | Next Page >