Search Results

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

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

  • What does the '&' operator do in C++?

    - by rascher
    n00b question. I am a C guy and I'm trying to understand some C++ code. I have the following function declaration: int foo(const string &myname) { cout << "called foo for: " << myname << endl; return 0; } How does the function signature differ from the equivalent C: int foo(const char *myname) Is there a difference between using string *myname vs string &myname? What is the difference between & in C++ and * in C to indicate pointers? Similarly: const string &GetMethodName() { ... } What is the & doing here? Is there some website that explains how & is used differently in C vs C++?

    Read the article

  • what does this C++ line of code mean "sol<?=f((1<<n)-1,i,0)+abs(P[i])*price;"

    - by KItis
    Could anyone help me to understand following line of code. sol I am studying an algorithm written using c++ and it has following operator " following is the error message returned. Hello.cpp: In function ‘int main()’: Hello.cpp:115: error: ‘memset’ was not declared in this scope Hello.cpp:142: error: expected primary-expression before ‘?’ token Hello.cpp:142: error: expected primary-expression before ‘=’ token Hello.cpp:142: error: expected ‘:’ before ‘;’ token Hello.cpp:142: error: expected primary-expression before ‘;’ token may be " Thanks in advance for the time you spent reading this post.

    Read the article

  • What do you call the << operator in Ruby when it's used for appending stuff?

    - by more or less
    In other contexts I know this << is called the bitshift operator. Is there a name for it when it's just used for append operations like you would do in an array or string (not sure what else you can append with it)? I'd like to be able to use an English word to refer to it instead of saying "you know, the operator with the two left arrows that's not really the left bitshift operator".

    Read the article

  • Operator== in derived class never gets called.

    - by Robin Welch
    Can someone please put me out of my misery with this? I'm trying to figure out why a derived operator== never gets called in a loop. To simplify the example, here's my Base and Derived class: class Base { // ... snipped bool operator==( const Base& other ) const { return name_ == other.name_; } }; class Derived : public Base { // ... snipped bool operator==( const Derived& other ) const { return ( static_cast<const Base&>( *this ) == static_cast<const Base&>( other ) ? age_ == other.age_ : false ); }; Now when I instantiate and compare like this ... Derived p1("Sarah", 42); Derived p2("Sarah", 42); bool z = ( p1 == p2 ); ... all is fine. Here the operator== from Derived gets called, but when I loop over a list, comparing items in a list of pointers to Base objects ... list<Base*> coll; coll.push_back( new Base("fred") ); coll.push_back( new Derived("sarah", 42) ); // ... snipped // Get two items from the list. Base& obj1 = **itr; Base& obj2 = **itr2; cout << obj1.asString() << " " << ( ( obj1 == obj2 ) ? "==" : "!=" ) << " " << obj2.asString() << endl; Here asString() (which is virtual and not shown here for brevity) works fine, but obj1 == obj2 always calls the Base operator== even if the two objects are Derived. I know I'm going to kick myself when I find out what's wrong, but if someone could let me down gently it would be much appreciated.

    Read the article

  • Index, assignment and increment in one statement behaves differently in C++ and C#. Why?

    - by Ivan Zlatanov
    Why is this example of code behaving differently in c++ and C#. [C++ Example] int arr[2]; int index = 0; arr[index] = ++index; The result of which will be arr[1] = 1; [C# Example] int[] arr = new int[2]; int index = 0; arr[index] = ++index; The result of which will be arr[0] = 1; I find this very strange. Surely there must be some rationale for both languages to implement it differently? I wonder what would C++/CLI output?

    Read the article

  • What are the best practices for implementing the == operator for a class in C#?

    - by remio
    While implementing an == operator, I have the feeling that I am missing some essential points. Hence, I am searching some best practices around that. Here are some related questions I am thinking about: How to cleanly handle the reference comparison? Should it be implemented through a IEquatable<T>-like interface? Or overriding object.Equals? And what about the != operator? (this list might not be exhaustive).

    Read the article

  • Swig C++ Lua Pass class by reference

    - by Jeremy
    I don't know why I'm having a hard time with this. All I want to do is this: class foo { public: foo(){} ~foo(){} float a,b; }; class foo2 { public: foo2(){} foo2(const foo &f){*this = f;} ~foo2(){} void operator=(const foo& f){ x = f.a; y = f.b; } float x,y; }; /* Usage(cpp): foo f; foo2 f2(f); //or using the = operator f2 = f; */ The problem I'm having is that, after swigging this code, I can't figure out how to make the lua script play nice. /* Usage(lua) f = example.foo() f2 = example.foo2(f) --error */ The error I get is "Wrong arguments for overloaded function 'new_Foo2'": Possible c/c++ prototypes are: foo2() foo2(foo const &) The same thing happens if I try and use do f2 = f. As I understand it everything is stored as a pointer so I did try adding an additional constructor that took a pointer to foo but to no avail.

    Read the article

  • == Operator and operands

    - by rahul
    I want to check whether a value is equal to 1. Is there any difference in the following lines of code Evaluated value == 1 1 == evaluated value in terms of the compiler execution

    Read the article

  • Using AND vs && in a for loop (Not related to precedence?)

    - by Peter
    Why is it that this code prints "Hello!" four times and then prints "1": <?php for ($i=1 AND $blah=1; $i<5; $i++) echo("Hello!"); echo($blah); ?> While this doesn't print out "Hello!" at all and then prints "1": <?php for ($i=1 && $blah=1; $i<5; $i++) echo("Hello!"); echo($blah); ?> I know AND and && have different precedences, but that doesn't seem to apply here. What am I missing? (I'm using a variant of the code above, since I will use $blah within the for loop, and I want to set the value for it). Thanks for any help!

    Read the article

  • How to make += operator keep the object reference?

    - by orloffm
    Say, I have a class: class M { public int val; And also a + operator inside it: public static M operator +(M a, M b) { M c = new M(); c.val = a.val + b.val; return c; } } And I've got a List of the objects of the class: List<M> ms = new List(); M obj = new M(); obj.val = 5; ms.Add(obj); Some other object: M addie = new M(); addie.val = 3; I can do this: ms[0] += addie; and it surely works as I expect - the value in the list is changed. But if I want to do M fromList = ms[0]; fromList += addie; it doesn't change the value in ms for obvious reasons. But intuitively I expect ms[0] to also change after that. Really, I pick the object from the list and then I increase it's value with some other object. So, since I held a reference to ms[0] in fromList before addition, I want still to hold it in fromList after performing it. Are there any ways to achieve that?

    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

  • problem with QDataStream & QDataStream::operator>> ( char *& s )

    - by yan bellavance
    QFile msnLogFile(item->data(Qt::UserRole).toString()); QDataStream logDataStream; if(msnLogFile.exists()){ msnLogFile.open(QIODevice::ReadOnly); logDataStream.setDevice(&msnLogFile); QByteArray logBlock; logDataStream >> logBlock; } This code doesnt work. The QByte that results is empty. Same thing if I use a char* . Oddely enough the same code works in another program. Im tying to find the difference between both. This works if i use int,uint, quint8, etc

    Read the article

  • Algorithm for bitwise fiddling

    - by EquinoX
    If I have a 32-bit binary number and I want to replace the lower 16-bit of the binary number with a 16-bit number that I have and keep the upper 16-bit of that number to produce a new binary number.. how can I do this using simple bitwise operator? For example the 32-bit binary number is: 1010 0000 1011 1111 0100 1000 1010 1001 and the lower 16-bit I have is: 0000 0000 0000 0001 so the result is: 1010 0000 1011 1111 0000 0000 0000 0001 how can I do this?

    Read the article

  • C++ overide global operator comma gives error

    - by uray
    the second function gives error C2803 http://msdn.microsoft.com/en-us/library/zy7kx46x%28VS.80%29.aspx : 'operator ,' must have at least one formal parameter of class type. any clue? template<class T,class A = std::allocator<T>> class Sequence : public std::vector<T,A> { public: Sequence<T,A>& operator,(const T& a) { this->push_back(a); return *this; } Sequence<T,A>& operator,(const Sequence<T,A>& a) { for(Sequence<T,A>::size_type i=0 ; i<a.size() ; i++) { this->push_back(a.at(i)); } return *this; } }; //this works! template<typename T> Sequence<T> operator,(const T& a, const T&b) { Sequence<T> seq; seq.push_back(a); seq.push_back(b); return seq; } //this gives error C2803! Sequence<double> operator,(const double& a, const double& b) { Sequence<double> seq; seq.push_back(a); seq.push_back(b); return seq; }

    Read the article

  • Is there an exponent operator in C#?

    - by Charlie
    For example, does an operator exist to handle this? float Result, Number1, Number2; Number1 = 2; Number2 = 2; Result = Number1 (operator) Number2; In the past the ^ operator has served as an exponential operator in other languages, but in C# it is a bit-wise operator. Do I have to write a loop or include another namespace to handle exponential operations? If so, how do I handle exponential operations using non-integers?

    Read the article

  • Why is Decimal('0') > 9999.0 True in Python?

    - by parxier
    This is somehow related to my question Why is ''0 True in Python? In Python 2.6.4: >> Decimal('0') > 9999.0 True From the answer to my original question I understand that when comparing objects of different types in Python 2.x the types are ordered by their name. But in this case: >> type(Decimal('0')).__name__ > type(9999.0).__name__ False Why is Decimal('0') > 9999.0 == True then? UPDATE: I usually work on Ubuntu (Linux 2.6.31-20-generic #57-Ubuntu SMP Mon Feb 8 09:05:19 UTC 2010 i686 GNU/Linux, Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15) [GCC 4.4.1] on linux2). On Windows (WinXP Professional SP3, Python 2.6.4 (r264:75706, Nov 3 2009, 13:23:17) [MSC v.1500 32 bit (Intel)] on win32) my original statement works differently: >> Decimal('0') > 9999.0 False I even more puzzled now. %-(

    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

  • Performance difference in for loop condition?

    - by CSharperWithJava
    Hello all, I have a simple question that I am posing mostly for my curiousity. What are the differences between these two lines of code? (in C++) for(int i = 0; i < N, N > 0; i++) for(int i = 0; i < N && N > 0; i++) The selection of the conditions is completely arbitrary, I'm just interested in the differences between , and &&. I'm not a beginner to coding by any means, but I've never bothered with the comma operator. Are there performance/behavior differences or is it purely aesthetic? One last note, I know there are bigger performance fish to fry than a conditional operator, but I'm just curious. Indulge me. Edit Thanks for your answers. It turns out the code that prompted this question had misused the comma operator in the way I've described. I wondered what the difference was and why it wasn't a && operator, but it was just written incorrectly. I didn't think anything was wrong with it because it worked just fine. Thanks for straightening me out.

    Read the article

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