Questions tagged [const-reference]

131 questions
5
votes
1 answer

Will temporary object be deleted if there is no const reference to it?

Lets take a look to this two functions: std::string get_string() { std::string ret_value; // Calculate ret_value ... return ret_value; } void process_c_string(const char* s) { std::cout << s << endl; } And here are two possible…
Mihran Hovsepyan
  • 10,810
  • 14
  • 61
  • 111
5
votes
3 answers

Returning reference of a temporary object from a function

Consider the following code - #include #include const int & retRef() { return 6; } int main() { const int& k = retRef(); printf("Value: %d\n", k); printf("Address: %p\n", &k); printf("Value: %d\n", k); …
kuro
  • 3,214
  • 3
  • 15
  • 31
5
votes
2 answers

what if C++ class contains both const reference and non-const reference copy constructor?

snippet 1: #include using namespace std; class C{ public: C(){} C(const C& c){ cout<<"const copy constructor called"<
expoter
  • 1,622
  • 17
  • 34
5
votes
1 answer

return const reference of subclass

What I know I know that returning a const reference of a temporary object is ok! (like this example:) class A { public: virtual const A& clone () { return (A()); } virtual std::string name() const { return ("A"); } }; Returning temporary object…
CollioTV
  • 684
  • 3
  • 13
5
votes
1 answer

assigning a temp to a const ref member causes a segmentation fault

better explained by an example: tok.h #include static const char* defaultDelim = ".,;"; class Tokenizer { public: Tokenizer(): // 'delim' is the const ref member that is initialized by the temp string delim(…
davka
  • 13,974
  • 11
  • 61
  • 86
4
votes
2 answers

What is the lifetime of the class data member which const reference to a rvalue?

Generally this discussion is up to the local function variable only: void foo (const int &i) { // use i till foo() ends } foo(3); But, does this rule applies to the class member also ? struct A { const int &a; A () : a(3) {} // version 1 A…
iammilind
  • 68,093
  • 33
  • 169
  • 336
4
votes
3 answers

On the weak semantics of references-to-const (and pointers-to-const)

This has probably been already asked. Why is it allowed to assign a reference-to-const to a non-const variable? Why is this allowed int mut {0}; const int & r_to_c {mut}; mut = 1; // now r_to_c changed to 1! // But it was supposed to be a reference…
Michele Piccolini
  • 2,634
  • 16
  • 29
4
votes
4 answers

Is there any C++ compiler which can issue a warning for a dangling reference?

Given the following code, where x is a dangling const reference to a vanished object, and is therefore undefined behavior. auto get_vec() { return std::vector{1,2,3,4,5}; } const auto& x = get_vec().back(); It seems like neither GCC 7.3, Clang…
Viktor Sehr
  • 12,825
  • 5
  • 58
  • 90
4
votes
2 answers

Why the overloaded method with const reference return value is not called?

Consider the following piece of code: #include using namespace std; class A { private: int x; public: int& get_ref() { cerr << "non const" << endl; return x; } const int& get_ref() const { …
MikeL
  • 2,369
  • 2
  • 24
  • 38
4
votes
1 answer

const auto& for storing functions results, is it worthwhile?

Let assume that we have a function that returns a complex object like std::string: std::string find_path(const std::string& filename); Is it worthwhile to store the result of calling that method in the const auto&? void do_sth() { //... const…
Greg Rynkowski
  • 556
  • 6
  • 20
4
votes
5 answers

Adding class functionality via composition

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…
A.L.
  • 1,133
  • 1
  • 12
  • 19
4
votes
1 answer

What happens in the stack when a const reference is bound to a temporary?

The C++ standard allows to bind const references to rvalues, therefore extending the lifetime of the temporary until the reference goes out of scope. However, I cannot figure out how this is actually compiled, let me explain with an…
Samuel Navarro Lou
  • 1,168
  • 6
  • 17
4
votes
1 answer

Life extension of temporary by const reference

C++ I'm trying to see how const references prolong the lifetime of temporaries. I'm running the code from the snippet in one of the answers to What are the differences between pointer variable and reference variable in C++? and got conflicting…
4
votes
2 answers

automatically use const-ref by big parameters

When I have following pseudo-class: template class tmplClass { void doSomething(T input); }; Is there a way to change void doSomething(T input) to void doSomething(const T& input) when sizeof(T) is larger the the system…
Uroc327
  • 1,379
  • 2
  • 10
  • 28
3
votes
2 answers

C++ : const references and initialization order

I am wondering if I am using the good approach in the following : I want to construct a parent class (class A), this class should own an instance of a given "Foo" class I want the parent class to own a child class member (class B) and this member…
Pascal T.
  • 3,866
  • 4
  • 33
  • 36
1 2
3
8 9