Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of '&' operator before a function name in C++

A reference defines an alternative name for an object. A reference type “refers to” another type. We define a reference type by writing a declarator of the form &d, where d is the name being declared.

The next thing is a reference is not an object. Instead, a reference is just another name for an already existing object. So we'll use these references to pass the parameter by reference so that it directly effect the actual parameters.

Question: What happens when we use a reference (&) before a function name?

I'm a little bit confused, as in my opinion it will return the alias of return (variable name). Or am I wrong?.

std::vector<std::string> &split(const std::string &s, char delim, 
                                std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}
like image 699
smali Avatar asked May 21 '14 07:05

smali


People also ask

How do we use a semicolon?

Use a semicolon to join two related independent clauses in place of a comma and a coordinating conjunction (and, but, or, nor, for, so, yet). Make sure when you use the semicolon that the connection between the two independent clauses is clear without the coordinating conjunction.

Should I use a colon or semicolon?

The primary use of semicolons is to join two main clauses. The difference between semicolons and colons is that colons can combine two independent clauses, but their primary use is to join independent clauses with a list or a noun.

What is a semicolon example?

When to Use a Semicolon. A semicolon (;) is a punctuation mark that has two main functions: Semicolons separate items in a complex list. For example, The Council is comprised of ten members: three from Sydney, Australia; four from Auckland, New Zealand; two from Suva, Fiji; and one from Honiara, Solomon Islands.

Where do you use commas and semicolons?

When a comma separates two complete sentences joined by a conjunction (and, but, or, nor, for, so, or yet) the comma and the conjunction can be replaced with a semicolon. I ate dinner, and I went to the movies. = I ate dinner; I went to the movies. She finished top of her class, but she was struggling to find work.


2 Answers

In C++, when the ref-sign (&) is used before the function name in the declaration of a function it is associated with the return value of the function and means that the function will return by reference.

int& foo(); // Function will return an int by reference.

When not used within a declaration context, putting the ref-sign before a function name results in calling the address-of operator returning the address of the function. This can be used to e.g. create a pointer to a function.

// Some function.
int sum(int a, int b) {
    return a + b;
}

int main() {
    // Declare type func_ptr_t as pointer to function of type int(int, int).
    using func_ptr_t = std::add_pointer<int(int, int)>::type;

    func_ptr_t func = &sum; // Declare func as pointer to sum using address-of.
    int sum = func(1, 2);   // Initialize variable sum with return value of func.
}

In C, the only use of & is for the address-of operator. References does not exist in the C language.

like image 90
Felix Glas Avatar answered Sep 20 '22 09:09

Felix Glas


'// return the plural version of word if ctr is greater than 1
string make_plural(size_t ctr, const string &word,
                           const string &ending)
{
    return (ctr > 1) ? word + ending : word;
}'

The return type of this function is string, which means the return value is copied to the call site. This function returns a copy of word, or it returns an unnamed temporary string that results from adding word and ending.

As with any other reference, when a function returns a reference, that reference is just another name for the object to which it refers. As an example, consider a function that returns a reference to the shorter of its two string parameters:

'// return a reference to the shorter of two strings
const string &shorterString(const string &s1, const string
&s2)
{
    return s1.size() <= s2.size() ? s1 : s2;
}'

The parameters and return type are references to const string. The strings are not copied when the function is called or when the result is returned.

Never Return a Reference or Pointer to a Local Object

When a function completes, its storage is freed. After a function terminates, references to local objects refer to memory that is no longer valid:

like image 45
smali Avatar answered Sep 18 '22 09:09

smali