Coming soon - Get a detailed view of why an account is flagged as spam!
view details

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.

2
Reference as parameter in function question
Post Flair (click to view more posts with a particular flair)
Post Body

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
  1. It makes no difference whether you give a normal variable or a reference to a function that expects a normal one

  2. 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?

Author
Account Strength
40%
Account Age
9 months
Verified Email
Yes
Verified Flair
No
Total Karma
93
Link Karma
76
Comment Karma
17
Profile updated: 1 month ago
Posts updated: 3 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
6 months ago