Search Results

Search found 2727 results on 110 pages for 'operator overloading'.

Page 19/110 | < Previous Page | 15 16 17 18 19 20 21 22 23 24 25 26  | Next Page >

  • Using overloaded operator== in a generic function

    - by Dimitri C.
    Consider the following code: class CustomClass { public CustomClass(string value) { m_value = value; } public static bool operator==(CustomClass a, CustomClass b) { return a.m_value == b.m_value; } public static bool operator!=(CustomClass a, CustomClass b) { return a.m_value != b.m_value; } public override bool Equals(object o) { return m_value == (o as CustomClass).m_value; } public override int GetHashCode() { return 0; /* not needed */ } string m_value; } class G { public static bool enericFunction1<T>(T a1, T a2) where T : class { return a1.Equals(a2); } public static bool enericFunction2<T>(T a1, T a2) where T : class { return a1==a2; } } Now when I call both generic functions, one succeeds and one fails: var a = new CustomClass("same value"); var b = new CustomClass("same value"); Debug.Assert(G.enericFunction1(a, b)); // Succeeds Debug.Assert(G.enericFunction2(a, b)); // Fails Apparently, G.enericFunction2 executes the default operator== implementation instead of my override. Can anybody explain why this happens?

    Read the article

  • Trying to overload + operator

    - by FrostyStraw
    I cannot for the life of me understand why this is not working. I am so confused. I have a class Person which has a data member age, and I just want to add two people so that it adds the ages. I don't know why this is so hard, but I'm looking for examples and I feel like everyone does something different, and for some reason NONE of them work. Sometimes the examples I see have two parameters, sometimes they only have one, sometimes the parameters are references to the object, sometimes they're not, sometimes they return an int, sometimes they return a Person object. Like..what is the most normal way to do it? class Person { public: int age; //std::string haircolor = "brown"; //std::string ID = "23432598"; Person(): age(19) {} Person operator+(Person&) { } }; Person operator+(Person &obj1, Person &obj2){ Person sum = obj1; sum += obj2; return sum; } I really feel like overloading a + operator should seriously be the easiest thing in the world except I DON'T KNOW WHAT I AM DOING. I don't know if I'm supposed to create the overload function inside the class, outside, if it makes a difference, why if I do it inside it only allows one parameter, I just honestly don't get it.

    Read the article

  • Assign method in Scala.

    - by Lukasz Lew
    When this code is executed: var a = 24 var b = Array (1, 2, 3) a = 42 b = Array (3, 4, 5) b (1) = 42 I see three (five?) assignments here. What is the name of the method call that is called in such circumstances? Is it operator overloading?

    Read the article

  • Best practices regarding equals: to overload or not to overload?

    - by polygenelubricants
    Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[] args) { class Thing { final int x; Thing(int x) { this.x = x; } public int hashCode() { return x; } public boolean equals(Thing other) { return this.x == other.x; } } List<Thing> myThings = Arrays.asList(new Thing(42)); System.out.println(myThings.contains(new Thing(42))); // prints "false" } } Note that contains returns false!!! We seems to have lost our things!! The bug, of course, is the fact that we've accidentally overloaded, instead of overridden, Object.equals(Object). If we had written class Thing as follows instead, then contains returns true as expected. class Thing { final int x; Thing(int x) { this.x = x; } public int hashCode() { return x; } @Override public boolean equals(Object o) { return (o instanceof Thing) && (this.x == ((Thing) o).x); } } Effective Java 2nd Edition, Item 36: Consistently use the Override annotation, uses essentially the same argument to recommend that @Override should be used consistently. This advice is good, of course, for if we had tried to declare @Override equals(Thing other) in the first snippet, our friendly little compiler would immediately point out our silly little mistake, since it's an overload, not an override. What the book doesn't specifically cover, however, is whether overloading equals is a good idea to begin with. Essentially, there are 3 situations: Overload only, no override -- ALMOST CERTAINLY WRONG! This is essentially the first snippet above Override only (no overload) -- one way to fix This is essentially the second snippet above Overload and override combo -- another way to fix The 3rd situation is illustrated by the following snippet: class Thing { final int x; Thing(int x) { this.x = x; } public int hashCode() { return x; } public boolean equals(Thing other) { return this.x == other.x; } @Override public boolean equals(Object o) { return (o instanceof Thing) && (this.equals((Thing) o)); } } Here, even though we now have 2 equals method, there is still one equality logic, and it's located in the overload. The @Override simply delegates to the overload. So the questions are: What are the pros and cons of "override only" vs "overload & override combo"? Is there a justification for overloading equals, or is this almost certainly a bad practice?

    Read the article

  • How is <tgmath.h> implemented?

    - by sync
    C doesn't have (to the best of my knowledge) overloading or templates, right? So how can a set of type-agnostic functions with the same name exist in plain ol' C? The usual compile-time trickery would involve a whole bunch of macros, wouldn't it?

    Read the article

  • Table and Column names causing problems

    - by craig
    I have an issue when the T4 linq templates generate the classes for my MySql db using subsonic 3. It looks like one of our table names "operator" is causing problems in the Context.cs generated class. In the following line of code in Context.cs Visual Studio sees <operator> as a c# operator and generates a compilation error of "Type expected" public Query<operator> operators { get; set; } Is there anyway I can work around this without having to rename my database table and column names? For example hard coding something in Settings.ttinclude to use or map different names to specific db tables and columns?

    Read the article

  • explicit copy constructor or implicit parameter by value

    - by R Samuel Klatchko
    I recently read (and unfortunately forgot where), that the best way to write operator= is like this: foo &operator=(foo other) { swap(*this, other); return *this; } instead of this: foo &operator=(const foo &other) { foo copy(other); swap(*this, copy); return *this; } The idea is that if operator= is called with an rvalue, the first version can optimize away construction of a copy. So when called with a rvalue, the first version is faster and when called with an lvalue the two are equivalent. I'm curious as to what other people think about this? Would people avoid the first version because of lack of explicitness? Am I correct that the first version can be better and can never be worse?

    Read the article

  • What is the best signature for overloaded arithmetic operators in C++?

    - by JohnMcG
    I had assumed that the canonical form for operator+, assuming the existence of an overloaded operator+= member function, was like this: const T operator+(const T& lhs, const T& rhs) { return T(lhs) +=rhs; } But it was pointed out to me that this would also work: const T operator+ (T lhs, const T& rhs) { return lhs+=rhs; } In essence, this form transfers creation of the temporary from the body of the implementation to the function call. It seems a little awkward to have different types for the two parameters, but is there anything wrong with the second form? Is there a reason to prefer one over the other?

    Read the article

  • Make conversion to a native type explicit in C++

    - by Tal Pressman
    I'm trying to write a class that implements 64-bit ints for a compiler that doesn't support long long, to be used in existing code. Basically, I should be able to have a typedef somewhere that selects whether I want to use long long or my class, and everything else should compile and work. So, I obviously need conversion constructors from int, long, etc., and the respective conversion operators (casts) to those types. This seems to cause errors with arithmetic operators. With native types, the compiler "knows" that when operator*(int, char) is called, it should promote the char to int and call operator*(int, int) (rather than casting the int to char, for example). In my case it gets confused between the various built-in operators and the ones I created. It seems to me like if I could flag the conversion operators as explicit somehow, that it would solve the issue, but as far as I can tell the explicit keyword is only for constructors (and I can't make constructors for built-in types). So is there any way of marking the casts as explicit? Or am I barking up the wrong tree here and there's another way of solving this? Or maybe I'm just doing something else wrong...

    Read the article

  • Is it possible to implement events in C++?

    - by acidzombie24
    I wanted to implement a C# event in C++ just to see if i could do it. I got stuck, i know the bottom is wrong but what i realize my biggest problem is... How do i overload the () operator to be whatever is in T in this case int func(float)? I cant? can i? Can i implement a good alternative? #include <deque> using namespace std; typedef int(*MyFunc)(float); template<class T> class MyEvent { deque<T> ls; public: MyEvent& operator +=(T t) { ls.push_back(t); return *this; } }; static int test(float f){return (int)f; } int main(){ MyEvent<MyFunc> e; e += test; }

    Read the article

  • What does the caret operator in Python do?

    - by Fry
    I ran across the caret operator in python today and trying it out, I got the following output: >>> 8^3 11 >>> 8^4 12 >>> 8^1 9 >>> 8^0 8 >>> 7^1 6 >>> 7^2 5 >>> 7^7 0 >>> 7^8 15 >>> 9^1 8 >>> 16^1 17 >>> 15^1 14 >>> It seems to be based on 8, so I'm guessing some sort of byte operation? I can't seem to find much about this searching sites other than it behaves oddly for floats, does anybody have a link to what this operator does or can you explain it here?

    Read the article

  • Conditional Operator in SQL Where Clause

    - by Marc
    I'm wishing I could do something like the following in SQl Server 2005 (which I know isnt valid) for my where clause. Sometimes @teamID (passed into a stored procedure) will be the value of an existing teamID, otherwise it will always be zero and I want all rows from the Team table. I researched using Case and the operator needs to come before or after the entire statement which prevents me from having a different operator based on the value of @teamid. Any suggestions other than duplicating my select statements. declare @teamid int set @teamid = 0 Select Team.teamID From Team case @teamid when 0 then WHERE Team.teamID > 0 else WHERE Team.teamID = @teamid end

    Read the article

  • Can operator= may be not a member?

    - by atch
    Having construction in a form: struct Node { Node():left_(nullptr), right_(nullptr) { } int id_; Node* left_; Node* right_; }; I would like to enable syntax: Node parent; Node child; parent.right_ = child; So in order to do so I need: Node& operator=(Node* left, Node right); but I'm getting msg that operator= has to be a member fnc; Is there any way to circumvent this restriction?

    Read the article

  • Is a switch statement the fastest way to implement operator interpretation in Java

    - by Mordan
    Is a switch statement the fastest way to implement operator interpretation in Java public boolean accept(final int op, int x, int val) { switch (op) { case OP_EQUAL: return x == val; case OP_BIGGER: return x > val; case OP_SMALLER: return x < val; default: return true; } } In this simple example, obviously yes. Now imagine you have 1000 operators. would it still be faster than a class hierarchy? Is there a threshold when a class hierarchy becomes more efficient in speed than a switch statement? (in memory obviously not) abstract class Op { abstract public boolean accept(int x, int val); } And then one class per operator.

    Read the article

  • Flash function overloading in haXe

    - by Voxl
    I am having some trouble figuring out how to overload a function in Flash using haXe. I know that Flash does not allow overloads but can accept function parameters without a type declared, but I am unsure as how to replicate this trick in haXe.

    Read the article

  • Arrow operator (->) usage in C

    - by Mohit Deshpande
    I am currently learning C by reading a good beginner's book called "Teach Yourself C in 21 Days" (I have already learned Java and C# so I am moving at a much faster pace). I was reading the chapter on pointers and the - (arrow) operator came up without explanation. I think that it is used to call members and functions (like the equivalent of the . (dot) operator, but for pointers instead of members). But I am not entirely sure. Could I please get an explanation and a code sample?

    Read the article

  • What logic operator to use, as3?

    - by VideoDnd
    What operator or expression can I use that will fire on every number, including zero? I want a logic operator that will fire with ever number it receives. My animations pause at zero. This skips on zero if (numberThing> 0); This skips on 9 if (numberThing>> 0); This jitters 'fires quickly and goes back on count' if (numberThing== 0); EXPLANATION I'm catching split string values in a logic function, and feeding them to a series of IF, ELSE IF statements. I'm using this with a timer, so I can measure the discrepency. CODE • I GET VALUES FROM TIMER • STRING GOES TO TEXTFIELD 'substr' • NUMBER TRIGGERS TWEENS 'parseInt' • Goes to series of IF and ELSE IF statements

    Read the article

  • System.ServiceModel.CommunicationException on overloading webservice

    - by soldieraman
    I am load testing my webservice and get a System.ServiceModel.CommunicationException when I use 10 threads to communicate to it (without any sleep in between) - basically testing 10 conenctions at a time - through a windows application An error occurred while receiving the HTTP response to http://localhost/XXX/XXXService.asmx. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details. Why would this happen and how to best resolve it

    Read the article

< Previous Page | 15 16 17 18 19 20 21 22 23 24 25 26  | Next Page >