operator-keyword

How do I overload the operator * when my object is on the right side in C++?

穿精又带淫゛_ 提交于 2019-12-17 21:05:43
问题 I want to implement "operator * " overloading INSIDE my class, so I would be able to do the following: Rational a(1, 2), b; b = 0.5 * a; // b = 1/4 Notice that b is on the right side, is there a way to do such a thing inside "Rational" class? 回答1: No. You must define operator* as a free function. Of course, you could implement it in terms of a member function on the second argument. 回答2: Yes: class Rational { // ... friend Rational operator*(float lhs, Rational rhs) { rhs *= lhs; return rhs;

XPath operator “!=”. How does it work?

你离开我真会死。 提交于 2019-12-17 18:25:42
问题 XML document: <doc> <A> <Node>Hello!</Node> </A> <B> <Node/> </B> <C> </C> <D/> </doc> How would you evaluate the following XPath queries? /doc/A/Node != 'abcd' /doc/B/Node != 'abcd' /doc/C/Node != 'abcd' /doc/D/Node != 'abcd' I would expect ALL of these to evaluate to true . However, here are the results: /doc/A/Node != 'abcd' true /doc/B/Node != 'abcd' true /doc/C/Node != 'abcd' false /doc/D/Node != 'abcd' false Is this expected behavior? Or is it a bug with my XPath provider (jaxen)? 回答1:

Overriding == operator. How to compare to null? [duplicate]

烂漫一生 提交于 2019-12-17 15:14:38
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How do I check for nulls in an ‘==’ operator overload without infinite recursion? There is probably an easy answer to this...but it seems to be eluding me. Here is a simplified example: public class Person { public string SocialSecurityNumber; public string FirstName; public string LastName; } Let's say that for this particular application, it is valid to say that if the social security numbers match, and both

On OS X, simple C++ program gives incorrect results (which are a result of command-line options 'c++03' vs 'c++11')

独自空忆成欢 提交于 2019-12-17 13:56:19
问题 This simple program (when compiled on Linux) will CORRECTLY give two different answers based on whether it's compiled with -std=c++0x or not. Problem: I cannot reproduce the same thing on OS X (Mountain Lion, 10.8 SDK). What am I missing? #include <iostream> #include <sstream> class Thing : public std::ostringstream { public: Thing() : std::ostringstream() {} virtual ~Thing() { std::cerr << str(); } }; int main(int argc, const char * argv[]) { Thing() << "Hello" << std::endl; return 0; } To

bash set -e and i=0;let i++ do not agree

我们两清 提交于 2019-12-17 10:49:32
问题 the following script with debug option 'set -e -v' fails at the increment operator only when the variable has a prior value of zero. #!/bin/bash set -e -v i=1; let i++; echo "I am still here" i=0; let i++; echo "I am still here" i=0; ((i++)); echo "I am still here" bash (GNU bash, version 4.0.33(1)-release (x86_64-apple-darwin10) but also GNU bash, version 4.2.4(1)-release (x86_64-unknown-linux-gnu)) any ideas? 回答1: the answer to my question is not to use let (or shift , or...) but to use i=$

Order of execution in operator <<

六月ゝ 毕业季﹏ 提交于 2019-12-17 06:54:03
问题 I have difficulties in understanding the sequence of calls in the code below. I was expecting to see the output below A1B2 While I can see that the output I get is BA12 I thought that the call std::cout<< b->fooA() << b->fooB() << std::endl was equivalent to call std::cout.operator<<( b->fooA() ).operator<< ( b->fooB() ) but I can see that this is not the case. Can you help me understanding better how this does it work and the relationship with the global operator<< ? Is this last ever called

C++ overload operator [ ][ ]

我是研究僧i 提交于 2019-12-17 06:45:31
问题 I have class CMatrix, where is "double pointer" to array of values. class CMatrix { public: int rows, cols; int **arr; }; I simply need to access the values of matrix by typing: CMatrix x; x[0][0] = 23; I know how to do that using: x(0,0) = 23; But I really need to do that the other way. Can anyone help me with that? Please? Thank you guys for help at the end I did it this way... class CMatrix { public: int rows, cols; int **arr; public: int const* operator[]( int const y ) const { return

C++ [] array operator with multiple arguments?

ε祈祈猫儿з 提交于 2019-12-17 06:38:38
问题 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 回答1: Nope, you can't overload operator[] to accept multiple arguments. You instead can overload operator() . See How do I

C++ Double Address Operator? (&&)

一世执手 提交于 2019-12-17 05:31:32
问题 I'm reading STL source code and I have no idea what && address operator is supposed to do. Here is a code example from stl_vector.h : vector& operator=(vector&& __x) // <-- Note double ampersands here { // NB: DR 675. this->clear(); this->swap(__x); return *this; } Does "Address of Address" make any sense? Why does it have two address operators instead of just one? 回答1: This is C++11 code. In C++11, the && token can be used to mean an "rvalue reference". 回答2: && is new in C++11. int&& a means

C++ Double Address Operator? (&&)

烂漫一生 提交于 2019-12-17 05:31:04
问题 I'm reading STL source code and I have no idea what && address operator is supposed to do. Here is a code example from stl_vector.h : vector& operator=(vector&& __x) // <-- Note double ampersands here { // NB: DR 675. this->clear(); this->swap(__x); return *this; } Does "Address of Address" make any sense? Why does it have two address operators instead of just one? 回答1: This is C++11 code. In C++11, the && token can be used to mean an "rvalue reference". 回答2: && is new in C++11. int&& a means