Search Results

Search found 4206 results on 169 pages for 'equals operator'.

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

  • Operator+ for a subtype of a template classe.

    - by baol
    I have a template class that defines a subtype. I'm trying to define the binary operator+ as a template function, but the compiler cannot resolve the template version of the operator+. #include <iostream> template<typename other_type> struct c { c(other_type v) : cs(v) {} struct subtype { subtype(other_type v) : val(v) {} other_type val; } cs; }; template<typename other_type> typename c<other_type>::subtype operator+(const typename c<other_type>::subtype& left, const typename c<other_type>::subtype& right) { return typename c<other_type>::subtype(left.val + right.val); } // This one works // c<a>::subtype operator+(const c<a>::subtype& left, // const c<a>::subtype& right) // { return c<a>::subtype(left.val + right.val); } int main() { c<int> c1 = 1; c<int> c2 = 2; c<int>::subtype cs3 = c1.cs + c2.cs; std::cerr << cs3.val << std::endl; } I think the reason is because the compiler (g++4.3) cannot guess the template type so it's searching for operator+<int> instead of operator+. What's the reason for that? What elegant solution can you suggest?

    Read the article

  • Using Comparable to compare objects and sorting them in a TreeMap

    - by arjacsoh
    II cannot understand how should the natural ordering of class be "consistent with equals" when implementing the Comparable interface. I deteted a flaw in my program and therefore I deteced that in the documentantion of the interface Comparable. My problem is that although two Objects are considered as distinct on the base of equals method, the TreeMap structure treats them as equal and consequently does not accept the second insert. The sample code is: public class Car implements Comparable<Car> { int weight; String name; public Car(int w, String n) { weight=w; name=n; } public boolean equals(Object o){ if(o instanceof Car){ Car d = (Car)o; return ((d.name.equals(name)) && (d.weight==weight)); } return false; } public int hashCode(){ return weight/2 + 17; } public String toString(){ return "I am " +name+ " !!!"; } public int compareTo(Car d){ if(this.weight>d.weight) return 1; else if(this.weight<d.weight) return -1; else return 0; } /*public int compareTo(Car d){ return this.name.compareTo(d.name); }*/ } public static void main(String[] args) { Car d1 = new Car(100, "a"); Car d2 = new Car(110, "b"); Car d3 = new Car(110, "c"); Car d4 = new Car(100, "a"); Map<Car, Integer> m = new HashMap<Car, Integer>(); m.put(d1, 1); m.put(d2, 2); m.put(d3, 3); m.put(d4, 16); for(Map.Entry<Car, Integer> me : m.entrySet()) System.out.println(me.getKey().toString() + " " +me.getValue()); TreeMap<Car, Integer> tm = new TreeMap<Car, Integer>(m); System.out.println("After Sorting: "); for(Map.Entry<Car, Integer> me : tm.entrySet()) System.out.println(me.getKey().toString() + " " +me.getValue()); } The output is : I am a !!! 16 I am c !!! 3 I am b !!! 2 After Sorting: I am a !!! 16 I am c !!! 2 That is, that the object c has replaced (somewhat) object b. If I comment the original equals method and uncomment the second equals method, which compares the objects according name, the output is the expected: I am a !!! 16 I am c !!! 3 I am b !!! 2 After Sorting: I am a !!! 16 I am b !!! 2 I am c !!! 3 Why does it come around in this way and what should I alter in order to insert and sort different objects with some attributes of equal value in a TreeMap?

    Read the article

  • Enforce "equals" in an interface

    - by Fredrik
    I have an interface and I want everyone who implements this interface to implements an overrridden "equals" method. Is there a way to make sure that happens? The way I guess this will happen is that the class that implements my interface will automatically get the equals from Object therefore making the interface happy.

    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

  • Custom string class (C++)

    - by Sanctus2099
    Hey guys. I'm trying to write my own C++ String class for educational and need purposes. The first thing is that I don't know that much about operators and that's why I want to learn them. I started writing my class but when I run it it blocks the program but does not do any crash. Take a look at the following code please before reading further: class CString { private: char* cstr; public: CString(); CString(char* str); CString(CString& str); ~CString(); operator char*(); operator const char*(); CString operator+(const CString& q)const; CString operator=(const CString& q); }; First of all I'm not so sure I declared everything right. I tried googleing about it but all the tutorials about overloading explain the basic ideea which is very simple but lack to explain how and when each thing is called. For instance in my = operator the program calls CString(CString& str); but I have no ideea why. I have also attached the cpp file below: CString::CString() { cstr=0; } CString::CString(char *str) { cstr=new char[strlen(str)]; strcpy(cstr,str); } CString::CString(CString& q) { if(this==&q) return; cstr = new char[strlen(q.cstr)+1]; strcpy(cstr,q.cstr); } CString::~CString() { if(cstr) delete[] cstr; } CString::operator char*() { return cstr; } CString::operator const char* () { return cstr; } CString CString::operator +(const CString &q) const { CString s; s.cstr = new char[strlen(cstr)+strlen(q.cstr)+1]; strcpy(s.cstr,cstr); strcat(s.cstr,q.cstr); return s; } CString CString::operator =(const CString &q) { if(this!=&q) { if(cstr) delete[] cstr; cstr = new char[strlen(q.cstr)+1]; strcpy(cstr,q.cstr); } return *this; } For testing I used a code just as simple as this CString a = CString("Hello") + CString(" World"); printf(a); I tried debugging it but at a point I get lost. First it calls the constructor 2 times for "hello" and for " world". Then it get's in the + operator which is fine. Then it calls the constructor for the empty string. After that it get's into "CString(CString& str)" and now I'm lost. Why is this happening? After this I noticed my string containing "Hello World" is in the destructor (a few times in a row). Again I'm very puzzeled. After converting again from char* to Cstring and back and forth it stops. It never get's into the = operator but neither does it go further. printf(a) is never reached. I use VisualStudio 2010 for this but it's basically just standard c++ code and thus I don't think it should make that much of a difference

    Read the article

  • Why isn't my operator overloading working properly?

    - by Mithrax
    I have the following Polynomial class I'm working on: #include <iostream> using namespace std; class Polynomial { //define private member functions private: int coef[100]; // array of coefficients // coef[0] would hold all coefficients of x^0 // coef[1] would hold all x^1 // coef[n] = x^n ... int deg; // degree of polynomial (0 for the zero polynomial) //define public member functions public: Polynomial::Polynomial() //default constructor { for ( int i = 0; i < 100; i++ ) { coef[i] = 0; } } void set ( int a , int b ) //setter function { //coef = new Polynomial[b+1]; coef[b] = a; deg = degree(); } int degree() { int d = 0; for ( int i = 0; i < 100; i++ ) if ( coef[i] != 0 ) d = i; return d; } void print() { for ( int i = 99; i >= 0; i-- ) { if ( coef[i] != 0 ) { cout << coef[i] << "x^" << i << " "; } } } // use Horner's method to compute and return the polynomial evaluated at x int evaluate ( int x ) { int p = 0; for ( int i = deg; i >= 0; i-- ) p = coef[i] + ( x * p ); return p; } // differentiate this polynomial and return it Polynomial differentiate() { if ( deg == 0 ) { Polynomial t; t.set ( 0, 0 ); return t; } Polynomial deriv;// = new Polynomial ( 0, deg - 1 ); deriv.deg = deg - 1; for ( int i = 0; i < deg; i++ ) deriv.coef[i] = ( i + 1 ) * coef[i + 1]; return deriv; } Polynomial Polynomial::operator + ( Polynomial b ) { Polynomial a = *this; //a is the poly on the L.H.S Polynomial c; for ( int i = 0; i <= a.deg; i++ ) c.coef[i] += a.coef[i]; for ( int i = 0; i <= b.deg; i++ ) c.coef[i] += b.coef[i]; c.deg = c.degree(); return c; } Polynomial Polynomial::operator += ( Polynomial b ) { Polynomial a = *this; //a is the poly on the L.H.S Polynomial c; for ( int i = 0; i <= a.deg; i++ ) c.coef[i] += a.coef[i]; for ( int i = 0; i <= b.deg; i++ ) c.coef[i] += b.coef[i]; c.deg = c.degree(); for ( int i = 0; i < 100; i++) a.coef[i] = c.coef[i]; a.deg = a.degree(); return a; } Polynomial Polynomial::operator -= ( Polynomial b ) { Polynomial a = *this; //a is the poly on the L.H.S Polynomial c; for ( int i = 0; i <= a.deg; i++ ) c.coef[i] += a.coef[i]; for ( int i = 0; i <= b.deg; i++ ) c.coef[i] -= b.coef[i]; c.deg = c.degree(); for ( int i = 0; i < 100; i++) a.coef[i] = c.coef[i]; a.deg = a.degree(); return a; } Polynomial Polynomial::operator *= ( Polynomial b ) { Polynomial a = *this; //a is the poly on the L.H.S Polynomial c; for ( int i = 0; i <= a.deg; i++ ) for ( int j = 0; j <= b.deg; j++ ) c.coef[i+j] += ( a.coef[i] * b.coef[j] ); c.deg = c.degree(); for ( int i = 0; i < 100; i++) a.coef[i] = c.coef[i]; a.deg = a.degree(); return a; } Polynomial Polynomial::operator - ( Polynomial b ) { Polynomial a = *this; //a is the poly on the L.H.S Polynomial c; for ( int i = 0; i <= a.deg; i++ ) c.coef[i] += a.coef[i]; for ( int i = 0; i <= b.deg; i++ ) c.coef[i] -= b.coef[i]; c.deg = c.degree(); return c; } Polynomial Polynomial::operator * ( Polynomial b ) { Polynomial a = *this; //a is the poly on the L.H.S Polynomial c; for ( int i = 0; i <= a.deg; i++ ) for ( int j = 0; j <= b.deg; j++ ) c.coef[i+j] += ( a.coef[i] * b.coef[j] ); c.deg = c.degree(); return c; } }; int main() { Polynomial a, b, c, d; a.set ( 7, 4 ); //7x^4 a.set ( 1, 2 ); //x^2 b.set ( 6, 3 ); //6x^3 b.set ( -3, 2 ); //-3x^2 c = a - b; // (7x^4 + x^2) - (6x^3 - 3x^2) a -= b; c.print(); cout << "\n"; a.print(); cout << "\n"; c = a * b; // (7x^4 + x^2) * (6x^3 - 3x^2) c.print(); cout << "\n"; d = c.differentiate().differentiate(); d.print(); cout << "\n"; cout << c.evaluate ( 2 ); //substitue x with 2 cin.get(); } Now, I have the "-" operator overloaded and it works fine: Polynomial Polynomial::operator - ( Polynomial b ) { Polynomial a = *this; //a is the poly on the L.H.S Polynomial c; for ( int i = 0; i <= a.deg; i++ ) c.coef[i] += a.coef[i]; for ( int i = 0; i <= b.deg; i++ ) c.coef[i] -= b.coef[i]; c.deg = c.degree(); return c; } However, I'm having difficulty with my "-=" operator: Polynomial Polynomial::operator -= ( Polynomial b ) { Polynomial a = *this; //a is the poly on the L.H.S Polynomial c; for ( int i = 0; i <= a.deg; i++ ) c.coef[i] += a.coef[i]; for ( int i = 0; i <= b.deg; i++ ) c.coef[i] -= b.coef[i]; c.deg = c.degree(); // overwrite value of 'a' with the newly computed 'c' before returning 'a' for ( int i = 0; i < 100; i++) a.coef[i] = c.coef[i]; a.deg = a.degree(); return a; } I just slightly modified my "-" operator method to overwrite the value in 'a' and return 'a', and just use the 'c' polynomial as a temp. I've put in some debug print statement and I confirm that at the time of computation, both: c = a - b; and a -= b; are computed to the same value. However, when I go to print them, their results are different: Polynomial a, b; a.set ( 7, 4 ); //7x^4 a.set ( 1, 2 ); //x^2 b.set ( 6, 3 ); //6x^3 b.set ( -3, 2 ); //-3x^2 c = a - b; // (7x^4 + x^2) - (6x^3 - 3x^2) a -= b; c.print(); cout << "\n"; a.print(); cout << "\n"; Result: 7x^4 -6x^3 4x^2 7x^4 1x^2 Why is my c = a - b and a -= b giving me different results when I go to print them?

    Read the article

  • Conditional Operator Example

    - by mbcrump
    If you haven’t taken the time to learn conditional operators, then now is the time. I’ve added a quick and dirty example for those on the forums.   Code Snippet using System; using System.Net.Mail; using System.Net; using System.Globalization; using System.Windows.Forms;   class Demo {     //Please use conditional statements in your code. See example below.       public static void Main()     {         int dollars = 10;           //Bad Coder Bad !!! Don't do this         if (dollars == 1)         {             Console.WriteLine("Please deposit {0} dollar.", dollars);         }         else         {             Console.WriteLine("Please deposit {0} dollars.", dollars);         }             //Good Coder Good !!! Do this         Console.WriteLine("Please deposit {0} dollar{1}.", dollars, dollars == 1 ? ' ' : 's');         //                                                          expression   ? true : false           Console.ReadLine();          } }

    Read the article

  • Showplan Operator of the Week – BookMark/Key Lookup

    Fabiano continues in his mission to describe the major Showplan Operators used by SQL Server's Query Optimiser. This week he meets a star, the Key Lookup, a stalwart performer, but most famous for its role in ill-performing queries where an index does not 'cover' the data required to execute the query. If you understand why, and in what circumstances, key lookups are slow, it helps greatly with optimising query performance.

    Read the article

  • LINQ and the use of Repeat and Range operator

    - by vik20000in
    LINQ is also very useful when it comes to generation of range or repetition of data.  We can generate a range of data with the help of the range method.     var numbers =         from n in Enumerable.Range(100, 50)         select new {Number = n, OddEven = n % 2 == 1 ? "odd" : "even"}; The above query will generate 50 records where the record will start from 100 till 149. The query also determines if the number is odd or even. But if we want to generate the same number for multiple times then we can use the Repeat method.     var numbers = Enumerable.Repeat(7, 10); The above query will produce a list with 10 items having the value 7. Vikram

    Read the article

  • Operator of the Week - Spools, Eager Spool

    For the fifth part of Fabiano's mission to describe the major Showplan Operators used by SQL Server's Query Optimiser, he introduces the spool operators and particularly the Eager Spool, explains blocking and non-blocking and then describes how the Halloween Problem is avoided.

    Read the article

  • Showplan Operator of the week - Assert

    As part of his mission to explain the Query Optimiser in practical terms, Fabiano attempts the feat of describing, one week at a time, all the major Showplan Operators used by SQL Server's Query Optimiser to build the Query Plan. He starts with Assert

    Read the article

  • Ruby: if statement using regexp and boolean operator [migrated]

    - by bev
    I'm learning Ruby and have failed to make a compound 'if' statement work. Here's my code (hopefully self explanatory) commentline = Regexp.new('^;;') blankline = Regexp.new('^(\s*)$') if (line !~ commentline || line !~ blankline) puts line end the variable 'line' is gotten from reading the following file: ;; alias filename backupDir Prog_i Prog_i.rb ./store Prog_ii Prog_ii.rb ./store This fails and I'm not sure why. Basically I want the comment lines and blank lines to be ignored during the processing of the lines in the file. Thanks for your help.

    Read the article

  • Best Practice: What can be the hashCode() method implementation if custom field used in equals() method are null?

    - by goodspeed
    What is the best practice to return a value for hashCode() method if custom field used in equals are null ? I have a situation, where equals() override is implemented using custom fields. Usually it it is better to override hashCode() also using that custom fields used in equals(). But if all the custom fields used in equals() are null, then what would be the best implementation for hashCode()? Example: class Person { private String firstName; private String lastName; public String getFirstName() { return firstName; } public String getLastName() { return lastName; } @Override public boolean equals(Object object) { boolean result = false; if (object == null || object.getClass() != getClass()) { result = false; } else { Person person = (Person) object; if (this.firstName == person.getFirstName() && this.lastName == tiger.getLastName()) { result = true; } } return result; } @Override public int hashCode() { int hash = 3; if(this.firstName == null || this.lastName == null) { // <b>What is the best practice here, </b> // <b>is return super.hashCode() better ?</b> } hash = 7 * hash + this.firstName.hashCode(); hash = 7 * hash + this.lastName.hashCode(); return hash; } } is it required to check for null in hashCode() ? If yes, what should be returned if custom values are null ?

    Read the article

  • Showplan Operator of the Week – BookMark/Key Lookup

    Fabiano continues in his mission to describe the major Showplan Operators used by SQL Server's Query Optimiser. This week he meets a star, the Key Lookup, a stalwart performer, but most famous for its role in ill-performing queries where an index does not 'cover' the data required to execute the query. If you understand why, and in what circumstances, key lookups are slow, it helps greatly with optimising query performance.

    Read the article

  • How do I overload an operator for an enumeration in C#?

    - by ChrisHDog
    I have an enumerated type that I would like to define the , <, =, and <= operators for. I know that these operators are implictly created on the basis of the enumerated type (as per the documentation) but I would like to explictly define these operators (for clarity, for control, to know how to do it, etc...) I was hoping I could do something like: public enum SizeType { Small = 0, Medium = 1, Large = 2, ExtraLarge = 3 } public SizeType operator >(SizeType x, SizeType y) { } But this doesn't seem to work ("unexpected toke") ... is this possible? It seems like it should be since there are implictly defined operators. Any suggestions?

    Read the article

  • How does delete deal with pointer constness?

    - by aJ
    I was reading this question Deleting a const pointer and wanted to know more about delete behavior. Now, as per my understanding: delete expression works in two steps: invoke destructor then releases the memory (often with a call to free()) by calling operator delete. operator delete accepts a void*. As part of a test program I overloaded operator delete and found that operator delete doesn't accept const pointer. Since operator delete does not accept const pointer and delete internally calls operator delete, how does Deleting a const pointer work ? Does delete uses const_cast internally?

    Read the article

  • Why would the assignment operator ever do something different than its matching constructor?

    - by Neil G
    I was reading some boost code, and came across this: inline sparse_vector &assign_temporary(sparse_vector &v) { swap(v); return *this; } template<class AE> inline sparse_vector &operator=(const sparse_vector<AE> &ae) { self_type temporary(ae); return assign_temporary(temporary); } It seems to be mapping all of the constructors to assignment operators. Great. But why did C++ ever opt to make them do different things? All I can think of is scoped_ptr?

    Read the article

  • Why does javascript's "in" operator return true when testing if 0 exists in an array that doesn't co

    - by Mariano Peterson
    For example, this returns true, and makes sense: var x = [1,2]; 1 in x; // true This returns false, and makes sense: var x = [1,2]; 3 in x; // false However this returns true, and I don't understand why: var x = [1,2]; 0 in x; You can quickly test it by putting this in your browser's address bar: javascript:var x=[1,2]; alert(0 in x); Why does the "in" operator in Javascript return true when testing if "0" exists in array, even when the array doesn't appear to contain "0"?

    Read the article

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