operator-keyword

Is operator-> “chained” for pointers? [duplicate]

狂风中的少年 提交于 2019-11-29 14:57:02
问题 Possible Duplicate: Overloading operator -> Hi, I've seen that operator->() is chained (re-applied) after it is evaluated, for example: struct Bar { Bar() : m_str("Hello world!") {} const string* operator->() const { return &m_str; } string m_str; }; struct Foo { const Bar& operator->() const { return m_bar; } Bar m_bar; }; int main() { Foo f; cout << f->c_str() << endl; return 0; } works pretty fine, which requires three operator->() to be evaluated - Foo::operator->() , Bar::operator->()

Speed of Comparison operators

╄→尐↘猪︶ㄣ 提交于 2019-11-29 14:16:21
In languages such as... well anything, both operators for < and <= (and their opposites) exist. Which would be faster, and how are they interpreted? if (x <= y) { blah; } or if (x < y + 1) { blah; } Assuming no compiler optimizations (big assumption), the first will be faster, as <= is implemented by a single jle instruction, where as the latter requires an addition followed by a jl instruction. http://en.wikibooks.org/wiki/X86_Assembly/Control_Flow#Jump_if_Less I wouldn't worry about this at all as far as performance goes. Using C as an example, on a simple test I ran with GCC 4.5.1 targeting

Is it not possible to call C++ operators manually?

纵饮孤独 提交于 2019-11-29 11:46:01
问题 I'm trying to understand operators in C++ more carefully. I know that operators in C++ are basically just functions. What I don't get is, what does the function look like? Take for example: int x = 1; int y = 2; int z = x + y; How does the last line translate? Is it: 1. int z = operator+(x,y); or 2. int z = x.operator+(y); ? When I tried both of them, the compiler errors. Am I calling them wrong or are operators in C++ not allowed to be called directly? 回答1: Using C++ standardese, the

Commutative operator overloading + of 2 different objects

a 夏天 提交于 2019-11-29 10:54:50
I have 2 classes which represent a matrix: 1. RegularMatrix - O(n^2) representation 2. SparseMatrix - a matrix that is represented as linked list (without zeros). lets say i have: RegularMatrix a; SparseMatrix b; i want to be able to do: a+b; and also: b+a; so i'm overloading the + operator. My question is, since I want the addition to be commutative (a+b = b+a), do i need to implement 2 overloadings, one for each case? RegularMatrix operator+(const RegualarMatrix &, const SparseMatrix &); RegularMatrix operator+(const SparseMatrix & ,const RegualarMatrix &); or is there a general form which

Operator overloading and namespaces [duplicate]

假装没事ソ 提交于 2019-11-29 09:32:32
Possible Duplicate: Where should non-member operator overloads be placed? While browsing on SO, I often find questions or answer that involves overloading/defining a std::ostream& operator<<(std::ostream& os, const Foo& foo) or a Foo operator+(const Foo& l, const Foo& r) . While I know how and when (not) to write these operators, I'm confused about the namespace thing. If I have the following class: namespace bar { class Foo {}; } In which namespace should I write the different operator definitions ? // Should it be this namespace bar { std::ostream& operator<<(std::ostream& os, const Foo& foo

C++ overloading conversion operator for custom type to std::string

大兔子大兔子 提交于 2019-11-29 06:58:32
问题 I hope someone might be able to answer why the following doesn't work. Bear with me though, I am still very much a noob... I just cannot get to the bottom of why the following using namespace std; #include <string> #include <iostream> class testClass { public: operator char* () {return (char*)"hi";}; operator int () {return 77;}; operator std::string () {return "hello";}; }; int main() { char* c; int i; std::string s = "goodday"; testClass t; c = t; i = t; s = t; cout<< "char: " << c << " int

c++ friend function - operator overloading istream >>

≯℡__Kan透↙ 提交于 2019-11-29 06:09:36
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 optional_base = 10); // MEMBER FUNCTIONS size_t get_digit(size_t index) const; size_t get_used() const;

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

不羁的心 提交于 2019-11-29 03:54:49
This question already has an answer here: What is the meaning of prepended double colon “::”? 9 answers 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 = INPUT_MOUSE; Input.mi.dwFlags = MOUSEEVENTF_LEFTUP; ::SendInput(1,&Input,sizeof(INPUT)); } This example works without the ::

C++ const std::map reference fails to compile

不打扰是莪最后的温柔 提交于 2019-11-29 03:24:42
Is there a reason why passing a reference to a std::map as const causes the [] operator to break? I get this compiler error (gcc 4.2) when I use const: error: no match for ‘operator[]’ in ‘map[name]’ Here's the function prototype: void func(const char ch, std::string &str, const std::map<std::string, std::string> &map); And, I should mention that there is no problem when I remove the const keyword in front of std::map . If I've been instructed correctly, the [] operator will actually insert a new pair into the map if it doesn't find the key, which would of course explain why this happens, but

Angular 6 pipe RxJs operator to chain 3 dependant observables

橙三吉。 提交于 2019-11-29 01:21:09
问题 I have 3 dependent Rest API resources (lets say observables) like this: 1st observable produces one item as array of users, like this: getUsers(): Observable<User[]> [ { "id": 1, "name": "Peter", "surname": "Smith" }, { "id": 2, "name": "John", "surname": "Wayne" }, ... ] 2nd observable can be used to fetch addresses assigned to user, so the input parameter is User ID, and returns a one item as array of addresses: getUserAddresses(user_id: string): Observable<Address[]> [ { "id": 1, "city":