-1

I am learning c++ and trying to fully understand the deference between if I take an argument like this (const type& var) versus (type var). I my understanding that by passing the "reference" (const type& var) will cost less because doesn't require a "copy constructer" or "destroy constructer" which make sense. But:

  1. when I should use this because in some cases (type var) cost less. when I should use this because in some cases (type var) cost less.
  2. why we not use pointer instead?
  3. (beginner question) how can we pass "value" to a "reference"?

    class Human{
        private:
        friend void DisplayAge (const Human& Person);
        string name;
        int age;
    
       public:
        Human (string personName, int personAge):
        name(personName), age(personAge) {}
    };
    
    void DisplayAge(const Human& Person)
    {
        cout << Person.age << endl;
    }
    
    int main() {
        Human FirstMan("Adam", 25);
    
        cout << "Accessing private member age via friend function: ";
        DisplayAge(FirstMan);
    
        return 0;
    }
    
HTN3D
  • 13
  • 1
  • 2
  • This kind of question already answered:[here](https://stackoverflow.com/questions/2582797/why-pass-by-const-reference-instead-of-by-value), or [here](https://stackoverflow.com/questions/1567138/const-t-arg-vs-t-arg). – Lex Marchenko Oct 08 '19 at 20:59
  • Usually unless you *need* to make a copy and the type is not primitive it's best to pass a reference; references are safer than pointers and easier to use; What do you mean by pass value to reference? – kingW3 Oct 08 '19 at 21:01

3 Answers3

0

Using (const type& var) you will ensure that the object should not be modified, and only leave acces to constant methods. Using pointer does not guarantee that an object is passed (or an array of objects). Could you explain more the third question?

ma1t05
  • 1
  • 1
  • 1
0

Question 3 answer:
If I understand you correctly, passing value, that is not reference or pointer to the function that takes reference will give this function direct access to this variable, or also fields of the actual value that you passed to this function. Example:

void foo(int& ref)
{
    ref = 5;
}

void bar(int non_ref)
{
    non_ref = 0;
}
int main()
{
    int integer_value = 1;
    foo(integer_value);
    bar(integer_value);

    return 0;
}

The first function will affect passed value, but the second won't, cause the operation of this function applied to copy of passed value.

Lex Marchenko
  • 126
  • 1
  • 9
0

You should generally use references if you plan on continuing to use the value you are passing into a function after that function completes. As you have highlighted, sometimes it is more costly to pass a reference. This can happen if your data is very small (you normally wouldn't bother passing integers as arguments), or if you have an opportunity to move the contents of the variable (though this can be a more complicated topic).

Using references allows you to avoid copying data while still guaranteeing that the argument is valid, since unlike with pointers there is no null reference. References give you the opportunity of allowing the function to mutate the contents of your variable, or with const references you can prevent a function from modifying the variable you pass in. In general you should prefer to use references over pointers when you don't need features only available with a pointer, and prefer const references over references when you don't need mutation.

Chris Pearce
  • 666
  • 5
  • 16
  • Also, learned that in some cases using reference argument protects the object from "slicing" when you accidentally passing subclass to base-class. – HTN3D Oct 09 '19 at 21:22