const-reference

Returning const reference of an arraylist

删除回忆录丶 提交于 2019-12-03 23:27:58
I really admire java features and I don't want to give up using it for the next problem: I have a class that might be inherited, and inside of it is a private ArrayList arr; So the setter function is ok , but the getter function return arr; returns the reference to that variable which anyone capable of editing that whole array which I don't want and private wouldn't make any sense ! In C++ I would just return const arr; and it would return constant reference to the variable. I so much need the variable not to be cloned or manually copied because there are so many calculations that require to

Visual Studio is not creating temporary object when typecasting?

女生的网名这么多〃 提交于 2019-12-03 14:04:38
I'm using Visual Studio Express 2013 and is fooling around a bit trying to learn about different things in C++. I stumbled upon an interesting bug in the compiler where it doesn't seem to create a temporary object when explicitly type casting to the same type as the reference. #include <iostream> using namespace std; int main() { int number; // float number; number = 2; const int& plainref_i = number; const int& recastref_i = (int)number; // this goes wrong if number is int const float& plainref_f = number; const float& recastref_f = (float)number; // this goes wrong if number is float number

const reference to temporary vs. return value optimization

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 20:32:24
I'm aware of the fact that assigning an rvalue to a const lvalue reference extends the temporaries lifetime until the end of the scope. However, it is not clear to me when to use this and when to rely on the return value optimization. LargeObject lofactory( ... ) { // construct a LargeObject in a way that is OK for RVO/NRVO } int main() { const LargeObject& mylo1 = lofactory( ... ); // using const& LargeObject mylo2 = lofactory( ... ); // same as above because of RVO/NRVO ? } According to Scot Meyers' More Effective C++ (Item 20) the second method could be optimized by the compiler to

C++ Return value, reference, const reference

浪子不回头ぞ 提交于 2019-12-02 15:30:34
Can you explain to me the difference between returning value, reference to value, and const reference to value? Value: Vector2D operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } Not-const reference: Vector2D& operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } Const reference: const Vector2D& operator += (const Vector2D& vector) { this->x += vector.x; this->y += vector.y; return *this; } What is the benefit of this? I understand the sense behind const reference passing to function as you want to make

Function with parameter type that has a copy-constructor with non-const ref chosen?

妖精的绣舞 提交于 2019-12-02 00:43:05
Some time ago I was confused by the following behavior of some code when I wanted to write a is_callable<F, Args...> trait. Overload resolution won't call functions accepting arguments by non-const ref, right? Why doesn't it reject in the following because the constructor wants a Test& ? I expected it to take f(int) ! struct Test { Test() { } // I want Test not be copyable from rvalues! Test(Test&) { } // But it's convertible to int operator int() { return 0; } }; void f(int) { } void f(Test) { } struct WorksFine { }; struct Slurper { Slurper(WorksFine&) { } }; struct Eater { Eater(WorksFine)

What exactly happens when returning const reference to a local object?

故事扮演 提交于 2019-11-30 20:16:06
struct A { A(int) : i(new int(783)) { std::cout << "a ctor" << std::endl; } A(const A& other) : i(new int(*(other.i))) { std::cout << "a copy ctor" << std::endl; } ~A() { std::cout << "a dtor" << std::endl; delete i; } void get() { std::cout << *i << std::endl; } private: int* i; }; const A& foo() { return A(32); } const A& foo_2() { return 6; } int main() { A a = foo(); a.get(); } I know, returning references to local values is bad. But, on the other hand, const reference should extend a temporary object lifetime. This code produce an UB output. So no life extention. Why? I mean can someone

Implicit conversion : const reference vs non-const reference vs non-reference

天大地大妈咪最大 提交于 2019-11-30 13:28:15
问题 Consider this code, struct A {}; struct B { B(const A&) {} }; void f(B) { cout << "f()"<<endl; } void g(A &a) { cout << "g()" <<endl; f(a); //a is implicitly converted into B. } int main() { A a; g(a); } This compiles fine, runs fine. But if I change f(B) to f(B&) , it doesn't compile. If I write f(const B&) , it again compiles fine, runs fine. Why is the reason and rationale? Summary: void f(B); //okay void f(B&); //error void f(const B&); //okay I would like to hear reasons, rationale and

C++: Is it better to pass an enum as a value or as a const reference?

为君一笑 提交于 2019-11-30 08:21:31
There are sort of two related questions here: A) How is enum implemented? For example, if I have the code: enum myType { TYPE_1, TYPE_2 }; What is actually happening? I know that you can treat TYPE_1 and TYPE_2 as ints, but are they actually just ints? B) Based on that information, assuming that the enum passed in didn't need to be changed, would it make more sense to pass myType into a function as a value or as a const reference? For example, which is the better choice: void myFunction(myType x){ // some stuff } or void myFunction(const myType& x) { // some stuff } Speed wise it almost

Why is `const T&` not sure to be const?

雨燕双飞 提交于 2019-11-29 22:47:22
template<typename T> void f(T a, const T& b) { ++a; // ok ++b; // also ok! } template<typename T> void g(T n) { f<T>(n, n); } int main() { int n{}; g<int&>(n); } Please note: b is of const T& and ++b is ok! Why is const T& not sure to be const? NathanOliver Welcome to const and reference collapsing . When you have const T& , the reference gets applied to T , and so does the const . You call g like g<int&>(n); so you have specified that T is a int& . When we apply a reference to an lvalue reference, the two references collapse to a single one, so int& & becomes just int& . Then we get to the

C++: Is it better to pass an enum as a value or as a const reference?

戏子无情 提交于 2019-11-29 11:30:35
问题 There are sort of two related questions here: A) How is enum implemented? For example, if I have the code: enum myType { TYPE_1, TYPE_2 }; What is actually happening? I know that you can treat TYPE_1 and TYPE_2 as ints, but are they actually just ints? B) Based on that information, assuming that the enum passed in didn't need to be changed, would it make more sense to pass myType into a function as a value or as a const reference? For example, which is the better choice: void myFunction