Use of the & operator in C++ function signatures

后端 未结 9 949
刺人心
刺人心 2020-11-29 00:36

I\'m currently reading through Accelerated C++ and I realized I don\'t really understand how & works in function signatures.

int* ptr=#
         


        
相关标签:
9条回答
  • 2020-11-29 01:14

    It's a reference which allows the function to modify the passed string, unlike a normal string parameter where modification would not affect the string passed to the function.

    You will often see a parameter of type const string& which is done for performance purposes as a reference internally doesn't create a copy of the string.

    0 讨论(0)
  • 2020-11-29 01:20

    In the case of:

    int* ptr=#
    

    you are declaring a variable named ptr with a type of an int * (int pointer), and setting its value to the "address of the variable num" (&num). The "addressof" operator (&) returns a pointer.

    In the case of:

    void DoSomething(string& str)
    

    you are declaring the first parameter of the DoSomething() method to be of type "reference to string". Effectively, this is the C++ way of defining "pass-by-reference".

    Note that while the & operator operates similarly in these cases, it's not acting in the same way. Specifically, when used as an operator, you're telling the compiler to take the address of the variable specified; when used in a method signature, you're telling the compiler that the argument is a reference. And note as well, that the "argument as a reference" bit is different from having an argument that is a pointer; the reference argument (&) gets dereferenced automatically, and there's never any exposure to the method as to where the underlying data is stored; with a pointer argument, you're still passing by reference, but you're exposing to the method where the variable is stored, and potentially exposing problems if the method fails to do a dereference (which happens more often than you might think).

    0 讨论(0)
  • 2020-11-29 01:21

    You shouldn't know anything about pointers until you get to chapter 10 of Accelerated C++ !

    A reference creates another name, an alias, for something that exists elsewhere. That's it. There are no hidden pointers or addresses involved. Don't look behind the curtain!

    Think of a guy named Robert

    guy   Robert;
    

    Sometimes you may want to call him Bob

    guy& Bob = Robert;
    

    Now Bob and Robert both refer to the same guy. You don't get his address (or phone number), just another name for the same thing.

    In your function

    void DoSomething(string& str)
    {
      string copy=str;
    }
    

    it works exactly the same, str is another name for some string that exists somewhere else.

    Don't bother with how that happens, just think of a reference as a name for some object. The compiler has to figure out how to connect the names, you don't have to.

    0 讨论(0)
  • 2020-11-29 01:23

    A reference is not a pointer, they're different although they serve similar purpose. You can think of a reference as an alias to another variable, i.e. the second variable having the same address. It doesn't contain address itself, it just references the same portion of memory as the variable it's initialized from.

    So

    string s = "Hello, wordl";
    string* p = &s; // Here you get an address of s
    string& r = s; // Here, r is a reference to s    
    
    s = "Hello, world"; // corrected
    assert( s == *p ); // this should be familiar to you, dereferencing a pointer
    assert( s == r ); // this will always be true, they are twins, or the same thing rather
    
    string copy1 = *p; // this is to make a copy using a pointer
    string copy = r; // this is what you saw, hope now you understand it better.
    
    0 讨论(0)
  • 2020-11-29 01:26

    The & character in C++ is dual purpose. It can mean (at least)

    1. Take the address of a value
    2. Declare a reference to a type

    The use you're referring to in the function signature is an instance of #2. The parameter string& str is a reference to a string instance. This is not just limited to function signatures, it can occur in method bodies as well.

    void Example() {
      string s1 = "example";
      string& s2 = s1;  // s2 is now a reference to s1
    }
    

    I would recommend checking out the C++ FAQ entry on references as it's a good introduction to them.

    • https://isocpp.org/wiki/faq/references
    0 讨论(0)
  • 2020-11-29 01:26

    While pass by reference may be implemented by the compiler by passing the address as a pointer, semantically it has nothing to do with addresses or pointers. in simple terms it is merely an alias for a variable.

    C++ has a lot of cases where syntax is reused in different contexts with different semantics and this is one of those cases.

    0 讨论(0)
提交回复
热议问题