Why should I use reference variables at all

0 votes
I learnt Java as my first programming language, but now that I'm attending a different university, I'm also learning C++.

After switching from Java to C++, I studied about references and reference variables.  And how hazardous they may be, how to use caution when using them, and so forth.

Therefore, I have one clear thought in my head: Why even bother employing that kind of difficult, maybe problematic, stuff?

Is it worthwhile or simply a holdover from the days when RAM was only approximately 64MB in size?

Since pointers have been discussed in several responses, in my opinion, the idea is archaic.

I wouldn't even touch those equipment, with the exception of high-performance computing.
Aug 3 in C++ by Nicholas
• 5,940 points
32 views

1 answer to this question.

0 votes

References themselves are unrelated to the issue.

The issue is because C++ manages object lifetimes differently from run-time systems that employ garbage collectors, such as Java. 

There is no standard built-in garbage collector in C++. 

Both automatic (within local or global scope) and manual (explicitly allocated/deallocated in heap) object lifetimes are possible in  C++.

A C++ reference is nothing more than an object's alias. 

It has no knowledge of object lifespan (for the sake of efficiency). 

The coder must give it some thought. 

A reference bound to a temporary object is an exception; in this situation, the temporary object's lifespan is prolonged to include the lifetime of the bound reference.

References play a crucial role in the fundamental ideas of C++, and they are required for 90% of jobs. 

Otherwise, pointers must be used, which is typically far worse.

You can use references, for instance, when you need to give an object as a function parameter by reference rather than by value:

void f(A copyOfObj);       // Passed by value, f receives copy of passed A instance
void f(A& refToObj);       // Passed by ref, f receives passed A instance itself, modifiable
void f(const A& refToObj); // Passed by const ref, f receives passed A instance itself, non modifiable
answered Aug 5 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

When and why do I need to use cin.ignore() in C++?

In C++, I developed a simple application that requested the user to enter a number and then a string.  Surprisingly, when I ran the application, it never paused to ask for the string.  It simply ignored it.  After conducting some research on StackOverflow, I discovered that I needed to include the following line: cin.ignore(256, '\n'); before the line with the string input  That addressed the problem and allowed the software to run.  My issue is why C++ need the cin.ignore() line, and how can I forecast when I will need to use it. Here's the software I created: #include <iostream> #include <string> using namespace std; int main() { ...READ MORE

Jul 4 in C++ by Nicholas
• 5,940 points
47 views
0 votes
1 answer

how could I use the power function in c/c++ without pow(), functions, or recursion

It is part of a series.  Replace pow() with the previous iteration's value. There is no need for code to call pow ().  Pow(x, 5 * I - 1) and pow(-1, I - 1) may be formed since both have an int exponent dependent on the iterator I from the previous loop iteration. Example: Let f(x, i) = pow(x, 5 * i ...READ MORE

answered Jun 21 in C++ by Damon
• 4,960 points
242 views
0 votes
1 answer

How can I get all the unique keys in a multimap

This method worked for me. for( multimap<char,int>::iterator it ...READ MORE

answered Jun 27 in C++ by Damon
• 4,960 points
40 views
0 votes
0 answers

Why use #define instead of a variable

What is the purpose of the #define ...READ MORE

Jul 1 in C++ by Nicholas
• 5,940 points
15 views
0 votes
0 answers

What are the differences between a pointer variable and a reference variable?

What distinguishes a reference variable from a ...READ MORE

Jul 11 in C++ by Nicholas
• 5,940 points
31 views
0 votes
1 answer

Explanation of function pointers

It's a little perplexing. Function type and pointer to function type are distinct kinds (no more similar than int and pointer to int).  However, in virtually all cases, a function type decays to a reference to a function type.  In this context, rotting roughly refers to conversion (there is a difference between type conversion and decaying, but you are probably not interested in it right now). What matters is that practically every time you use a function type, you end up with a reference to the function type.  But take note of the nearly - almost every time is not always! And there are rare circumstances where it does not. typedef void(functionPtr)(int); functionPtr fun = function; This code tries to clone one function to another (not the pointer! the function!)  However, this is not feasible since functions in C++ cannot be copied.  The compiler does not let this, and I'm surprised you got it compiled (you say you got linker errors?) Now for the code: typedef void(functionPtr)(int); functionPtr function; function(5); function does ...READ MORE

answered Jun 21 in C++ by Damon
• 4,960 points
24 views
0 votes
1 answer

In C++ abs( *a - *b) does not return absolute value of negative number

On the first line, you reallocated *a, and it is now utilising that new value on the second line.  int origa = *a; *a = abs(origa + ...READ MORE

answered Jun 27 in C++ by Damon
• 4,960 points
23 views
0 votes
0 answers

C++ pointer to objects

Is it always necessary in C++ to  ...READ MORE

Jun 27 in C++ by Nicholas
• 5,940 points
25 views
0 votes
1 answer

What is a smart pointer and when should I use one?

A smart pointer is similar to a ...READ MORE

answered Jun 2 in C++ by Damon
• 4,960 points
29 views
0 votes
1 answer

Why would anyone use set instead of unordered_set?

Unordered sets must compensate for their O(1) ...READ MORE

answered Jun 1 in C++ by Damon
• 4,960 points
78 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP