This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I'm learning c and i'm at references at the moment and i'm trying to be thorough, in this article (https://www.geeksforgeeks.org/references-in-cpp/) in the second code block is the following example
// C Program to demonstrate
// Passing of references as parameters
#include <iostream>
using namespace std;
// Function having parameters as
// references
void swap(int& first, int& second)
{
int temp = first;
first = second;
second = temp;
}
// Driver function
int main()
{
// Variables declared
int a = 2, b = 3;
// function called
swap(a, b);
// changes can be seen
// printing both variables
cout << a << " " << b;
return 0;
}
My first thought is why can you pass a normal variable to a function that takes a reference as a parameter?
https://www.w3schools.com/cpp/cpp_references.asp :
string food =Â "Pizza";Â Â // food variable
string &meal = food;Â Â Â Â // reference to food
Shouldn't it be a reference variable like meal from above?
I decided to make a small test to see with both a string and a struct:
void Lesson83a(string varIn, string newValue) {
varIn = newValue;
}
void Lesson83b(string& varIn, string newValue) {
varIn = newValue;
}
struct Lesson83Struct {
string testString;
int testInt;
};
void Lesson83c(Lesson83Struct structIn, string newS, int newI) {
structIn.testString = newS;
structIn.testInt = newI;
}
void Lesson83d(Lesson83Struct& structIn, string newS, int newI) {
structIn.testString = newS;
structIn.testInt = newI;
}
void Lesson83() {
string testString = "Foo";
string& testStringRef = testString;
Lesson83a(testString, "1");
cout << testString << "\n";
Lesson83b(testString, "2");
cout << testString << "\n";
Lesson83a(testStringRef, "3");
cout << testString << "\n";
Lesson83b(testStringRef, "4");
cout << testString << "\n";
cout << "-----\n";
Lesson83Struct testStruct;
Lesson83Struct& testStructRef = testStruct;
testStruct.testString = "Foo";
testStruct.testInt = 0;
Lesson83c(testStruct, "1", 1);
cout << testStruct.testString << " " << testStruct.testInt << "\n";
Lesson83d(testStruct, "2", 2);
cout << testStruct.testString << " " << testStruct.testInt << "\n";
Lesson83c(testStructRef, "3", 3);
cout << testStruct.testString << " " << testStruct.testInt << "\n";
Lesson83d(testStructRef, "4", 4);
cout << testStruct.testString << " " << testStruct.testInt << "\n";
}
Foo
2
2
4
-----
Foo 0
2 2
2 2
4 4
It makes no difference whether you give a normal variable or a reference to a function that expects a normal one
a function that expects a reference can modify the variable send to it outside its scope regardless of it you pass it the variable directly or a reference to it
The first article states that one of the reasons to use references is to "Avoiding a copy of large structures" so this seems important to me, my question is what happens under the hood in the compiler? does it matter in which way you do it?
Subreddit
Post Details
- Posted
- 6 months ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/cpp_questio...