operator-keyword

vector::push_back vs vector::operator[]

流过昼夜 提交于 2019-11-29 00:43:31
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 only difference between these two or something else is there ? push_back creates a new element on the

new operator for memory allocation on heap

。_饼干妹妹 提交于 2019-11-28 22:22:16
问题 I was looking at the signature of new operator. Which is: void* operator new (std::size_t size) throw (std::bad_alloc); But when we use this operator, we never use a cast. i.e int *arr = new int; So, how does C++ convert a pointer of type void* to int* in this case. Because, even malloc returns a void* and we need to explicitly use a cast. 回答1: There is a very subtle difference in C++ between operator new and the new operator. (Read that over again... the ordering is important!) The function

c++ multiple definitions of operator<<

梦想的初衷 提交于 2019-11-28 22:21:53
问题 I am attempting to override the << operator for a class. The purpose is basically to implement a toString() like behavior for my class, so that sending it to cout will produce useful output. Using a dummy example, I have the code below. When I attempt to compile, I get the foollowing error: $ g++ main.cpp Rectangle.cpp /tmp/ccWs2n6V.o: In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CRectangle const&)': Rectangle.cpp:(.text+0x0): multiple definition of `operator<<

I keep getting a “The operator == is undefined for the argument type(s) boolean, int” and have no idea how to fix it

让人想犯罪 __ 提交于 2019-11-28 14:21:26
I keep getting a "The operator == is undefined for the argument type(s) boolean, int" in this bit of code at line 3: public void loadState(int i) { if (statesSaved[i] == 0) { return; } List list = TMIUtils.getMinecraft().h.at.e; for (int j = 0; j < 44; j++) { sx slot = (sx)list.get(j + 1); slot.c(null); ul itemstack = TMIUtils.copyStack(states[i][j]); if ((itemstack == null) || (itemstack.c < 0) || (itemstack.c >= sv.f.length) || (sv.f[itemstack.c] == null)) continue; slot.c(itemstack); } } I have no idea why this happens, since i have another class file with the same thing where there is no

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

China☆狼群 提交于 2019-11-28 13:59:51
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? 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. Yes: class Rational { // ... friend Rational operator*(float lhs, Rational rhs) { rhs *= lhs; return rhs; } }; Note: this is of course an abuse of the friend keyword. It should be a free function. Answer is no you

User Defined Conversions in C++

我的梦境 提交于 2019-11-28 12:07:57
Recently, I was browsing through my copy of the C++ Pocket Reference from O'Reilly Media, and I was surprised when I came across a brief section and example regarding user-defined conversion for user-defined types: #include <iostream> class account { private: double balance; public: account (double b) { balance = b; } operator double (void) { return balance; } }; int main (void) { account acc(100.0); double balance = acc; std::cout << balance << std::endl; return 0; } I've been programming in C++ for awhile, and this is the first time I've ever seen this sort of operator overloading. The book

PHP operator precedence “Undefined order of evaluation”?

喜夏-厌秋 提交于 2019-11-28 11:51:21
问题 http://www.php.net/manual/en/language.operators.precedence.php#example-115 <?php $a = 1; echo $a + $a++; // may print either 2 or 3 ?> The example from the php manual doesn't explain very well. Why isn't $a++ evaluated to 2 , and then added to 1 , so that it always becomes echo 1 + 2 // equals 3 ? I don't understand how it "may print either 2 or 3". I thought incremental ++ has "higher precedence" than addition + ? In other words, I don't understand why isn't it... $a = 1; 1) echo $a + $a++;

Operator to check file existence

不羁岁月 提交于 2019-11-28 09:16:41
问题 I want to create an operator .f. that checks whether a file exists so that I can write if (.f. filename) Then ... I have already written a function to do this, now have to create the interface. What would the constraints on e function arguments for having the mentioned functionality? 回答1: You can use the inquire intrinsic: module fileIO interface operator( .f. ) module procedure file_exists end interface contains function file_exists(filename) result(res) implicit none character(len=*),intent

Bash: How to use operator parameter expansion ${parameter@operator}?

北城以北 提交于 2019-11-28 07:21:47
问题 I've googled and tried so many things and never could get anything to work with ${parameter@operator}. All I find is more links to the same documentation. So I think a proper answer with practical examples would be very helpful to its understanding. The documentation says: ${parameter@operator} The expansion is either a transformation of the value of parameter or information about parameter itself, depending on the value of operator. Each operator is a single letter: Q The expansion is a

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

时光毁灭记忆、已成空白 提交于 2019-11-28 07:15:26
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)? Recommendation : Never use the != operator to compare inequality where one or both arguments are node-sets.