operator-keyword

Overloading operator ->

本小妞迷上赌 提交于 2019-12-18 12:52:59
问题 Here is my code example: class X { public: void f() {} }; class Y : public X { public: X& operator->() { return *this; } void f() {} }; int main() { Y t; t.operator->().f(); // OK t->f(); // error C2819: type 'X' does not have an overloaded member 'operator ->' // error C2232: '->Y::f' : left operand has 'class' type, use '.' } Why the compiler is trying to "move the responsibility" for operator-> from Y to X? When I implement X::op-> then I cannot return X there - compile error says

Bitwise AND, Bitwise Inclusive OR question, in Java

穿精又带淫゛_ 提交于 2019-12-18 12:28:29
问题 I've a few lines of code within a project, that I can't see the value of... buffer[i] = (currentByte & 0x7F) | (currentByte & 0x80); It reads the filebuffer from a file, stored as bytes, and then transfers then to buffer[i] as shown, but I can't understand what the overall purpose is, any ideas? Thanks 回答1: As the other answers already stated, (currentByte & 0x7F) | (currentByte & 0x80) is equivalent to (currentByte & 0xFF) . The JLS3 15.22.1 says this is promoted to an int : When both

Undefined method '>' for nil:NilClass <NoMethodError>

99封情书 提交于 2019-12-18 10:41:40
问题 Ok I do have the following code def update_state_actions states.each do |state| @state_turns[state.id] -= 1 if @state_turns[state.id] > 0 && state.auto_removal_timing == 1 end end now in the line of... @state_turns[state.id] -= 1 if @state_turns[state.id] > 0 && state.auto_removal_timing == 1 it says the error in 'block update_state_actions' : Undefined method '>' for nil:NilClass <NoMethodError> what is the cause of the error? how come > is considered as a method but it is a logical operator

Overloading operators in typedef structs (c++)

江枫思渺然 提交于 2019-12-18 10:09:32
问题 I want to make a typedef struct called pos (from position) that stores coordinates x and y. I am trying to overload some operators for this struct, but it does not compile. typedef struct { int x; int y; inline pos operator=(pos a) { x=a.x; y=a.y; return a; } inline pos operator+(pos a) { return {a.x+x,a.y+y}; } inline bool operator==(pos a) { if (a.x==x && a.y== y) return true; else return false; } } pos; I also wanted to know the difference between this: inline bool operator==(pos a) { if(a

Calling primitive operator-functions explicitly in C++

こ雲淡風輕ζ 提交于 2019-12-18 05:56:49
问题 int a, b, c; //do stuff. For e.g., cin >> b >> c; c = a + b; //works c = operator+(a,b); //fails to compile, 'operator+' not defined. This on the other hand works - class Foo { int x; public: Foo(int x):x(x) {} Foo friend operator+(const Foo& f, const Foo& g) { return Foo(f.x + g.x); } }; Foo l(5), m(10); Foo n = operator+(l,m); //compiles ok! Is it even possible to invoke operator+ (and other operators) of primitive types (like int) directly? If yes, how? If not, is there a C++ reference

c++ friend function - operator overloading istream >>

随声附和 提交于 2019-12-18 04:38:12
问题 My question is in regards to friend functions as well as overloading the << and >>. From my understanding I thought friend functions could (and should) access private member variables directly. However in the case I have here the compiler would only accept my .cxx file when I used "get" functions to obtain each private variable. Here is my header file class BigNum public: // CONSTRUCTORS and DESTRUCTORS BigNum(); BigNum(int num, size_t optional_base = 10); BigNum(const char strin[], size_t

Why is :: (scope) used with empty left-hand operand? [duplicate]

牧云@^-^@ 提交于 2019-12-18 04:04:13
问题 This question already has answers here : What is the meaning of prepended double colon “::”? (9 answers) Closed 12 months ago . I've seen this a few times now, and I've been scratching my head wondering why... As an example: (http://www.codeguru.com/forum/showthread.php?t=377394) void LeftClick ( ) { INPUT Input={0}; // left down Input.type = INPUT_MOUSE; Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN; ::SendInput(1,&Input,sizeof(INPUT)); // left up ::ZeroMemory(&Input,sizeof(INPUT)); Input.type =

Why is :: (scope) used with empty left-hand operand? [duplicate]

我与影子孤独终老i 提交于 2019-12-18 04:04:10
问题 This question already has answers here : What is the meaning of prepended double colon “::”? (9 answers) Closed 12 months ago . I've seen this a few times now, and I've been scratching my head wondering why... As an example: (http://www.codeguru.com/forum/showthread.php?t=377394) void LeftClick ( ) { INPUT Input={0}; // left down Input.type = INPUT_MOUSE; Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN; ::SendInput(1,&Input,sizeof(INPUT)); // left up ::ZeroMemory(&Input,sizeof(INPUT)); Input.type =

vector::push_back vs vector::operator[]

谁说我不能喝 提交于 2019-12-18 03:00:32
问题 Below in c++ program, include<iostream> #include<vector> using namespace std; int main() { vector<int> numbers; numbers.push_back(2); numbers.push_back(10); numbers.push_back(5); numbers.push_back(3); numbers.push_back(7); numbers[3] = 8; numbers[5] = 11; for(int i=0; i<numbers.size(); ++i) { cout<<" "<<numbers[i]; } } see it on ideone. here, numbers[3] is working but numbers[5] . It looks like, vector::operator[] doesn't increase the size of vector like vector::push_back. so, is this the

How does operator.itemgetter and sort() work in Python?

≡放荡痞女 提交于 2019-12-17 21:25:58
问题 I have the following code: # initialize a = [] # create the table (name, age, job) a.append(["Nick", 30, "Doctor"]) a.append(["John", 8, "Student"]) a.append(["Paul", 22, "Car Dealer"]) a.append(["Mark", 66, "Retired"]) # sort the table by age import operator a.sort(key=operator.itemgetter(1)) # print the table print(a) It creates a 4x3 table and then it sorts it by age. My question is, what exactly key=operator.itemgetter(1) does? Does the operator.itemgetter function return the item's value