CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2008
    Posts
    26

    [RESOLVED] What does a & after an argument/variable type signify?

    I've run across this code and have seen similar code before, but I'm not sure what it means
    Code:
    //--- file Person.h
    . . .
    class Person {
        private:
            char* _name;
            int   _id;
        public:
            Person& Person::operator=(const Person& p);
        . . .
    }
    
    //--- file Person.cpp
    . . .
    //=================================================== operator=
    Person& Person::operator=(const Person& p) {
        if (this != &p) {  // make sure not same object
            delete [] _name;                     // Delete old name's memory.
            _name = new char[strlen(p._name)+1]; // Get new space
            strcpy(_name, p._name);              // Copy new name
            _id = p._id;                         // Copy id
        }
        return *this;    // Return ref for multiple assignment
    }//end operator=
    Why is there a & after Person? What is it saying to the compiler?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: What does a & after an argument/variable type signify?

    Quote Originally Posted by Brownhead
    Why is there a & after Person? What is it saying to the compiler?
    That is the reference operator. The return value is a reference to a Person object, and the argument passed is a reference to a Person.

    References, pointers, etc. should be discussed in any elementary C++ book.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Apr 2008
    Posts
    26

    Re: What does a & after an argument/variable type signify?

    Thank you , I never learned about references.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured