const-reference

Returning const reference to local variable from a function

I have some questions on returning a reference to a local variable from a function: class A { public: A(int xx):x(xx) { printf("A::A()\n"); } }; const A& getA1() { A a(5); return a; } A& getA2() { A a(5); return a; } A getA3() { A a(5); return a; } int main() { const A& newA1 = getA1(); //1 A& newA2 = ge...

Is it possible to change the temporary object and to pass it as an argument?

Is it possible to change the temporary object and to pass it as an argument? struct Foo { Foo& ref() { return *this; } Foo& operator--() { /*do something*/; return *this; } // another members }; Foo getfoo() { return Foo(); } // return Foo() for example or something else void func_val(Foo x) {} void func_ref(const Foo & x) ...

Why does this work? Returning const references in C++

I am fooling around with C++ and const references and am confused why this code works: #include <iostream> class A { public: A() : a_(50) {} const int& getA() const { return a_; } private: const int a_; }; int main(int argc, char* argv[]) { A* a = new A(); const int& var = a->getA(); std::cout << var << std::en...

Reference initialization in C++

Hi, Can anybody explain to me why there is a difference between these two statements? class A{}; const A& a = A(); // correct A& b = A(); // wrong It says invalid initialization of non-const reference of type A& from a temporary of type A Why does const matter here? ...

How to return a const QString reference in case of failure?

Hi, consider the following code: const QString& MyClass::getID(int index) const { if (i < myArraySize && myArray[i]) { return myArray[i]->id; // id is a QString } else { return my_global_empty_qstring; // is a global empty QString } } How can I avoid to have an empty QString without changing the return type...

Does a const reference prolong the life of a temporary?

Why does this: #include <string> #include <iostream> using namespace std; class Sandbox { public: Sandbox(const string& n) : member(n) {} const string& member; }; int main() { Sandbox sandbox(string("four")); cout << "The answer is: " << sandbox.member << endl; return 0; } Give output of: The answer is: Ins...

Adding class functionality via composition

Hello, Suppose we have an abstract class Element from which classes Triangle and Quadrilateral are derived from. Suppose yet that these classes are used in conjunction with interpolation methods that depend on the shape of the element. So, basically we create an abstract class InterpolationElement from which we derive InterpolationTri...

const_reference or iterator for map (when not actually iterating)

I have some code that uses an iterator to loop through all elements of an unordered_map, but within that loop there are several other processes where I store iterators to particular elements in the map, and generally do lots of jumping around but not iterating (excluding the outermost iterator). It seemed to me that it may be more suita...