Thread: The '& operator'...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    30

    The '& operator'...

    What is the '& operator' and how does it work?

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    It can mean more than thing. However, I am assuming you are talking about the address of operator. When you have a variable, placing the ampersand (&) before the variable means you want the address of the variable instead of the actual value. For example:


    Code:
    #include <iostream.h>
    
    int main()
    {  int number=4;
        cout<<number;//will display 4
        cout<<&number;//will display some weird #, the address of number
        
        return 0;
    }

  3. #3
    Unregistered
    Guest
    actually the ampersand is after the variable:

    example:
    [code]
    T& operator [] ...

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    30
    Yeah, in the example im working with, the ampersand is used in fron of the variable:

    Code:
    T& operator []

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    It means a reference to the variable after it. Think of a reference like a pointer. However, the address it refers to cannot be reassigned. Also, there is no need to dereference it when using it. Basically, think of it as an alias. For example, a reference to William Clinton might be Bill Clinton. When refering to one, it modifies both.

    Code:
    #include <iostream.h>
    
    int main()
    { int number=4;
       int &numreference=number;
       cout<<number;//outputs 4
       cout<<numreference;//outputs 4
       number=7;
       cout<<number;//outputs 7
       cout<<numreference;//outputs 7
       
       return 0;
    }

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    BTW, your example means the function returns a reference to a T type.

  7. #7
    Unregistered
    Guest
    unary operator gives you the address as previously stated, useful for passing to functions and pointers, see examples above.

    as a binary operator it provides an alternative identity for a memory address, eg

    Code:
    int number = 4;
    int& sameNumber = number;    //& can be beside data type or identifier
    
    cout << number << endl;    //prints 4
    cout << sameNumber << endl;    //prints 4

  8. #8
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    to gamma:

    & operator: a bit operator (for binary operations)

    the binary operators:
    & : AND
    | : OR
    ^ : XOR

    if you have knowledge on how the boolean concept works, this shouldn't be a problem.

    when using the '&' (AND) operator, you will be working with binary data so if you are working with an int, the int will be operated though binary operations (it does this anyway since machine operations are based on 1's and 0's).
    so:
    1 & 1 = 1
    1 & 0 = 0
    0 & 0 = 0

    if you are working with a group of binaries,
    example: 9 + 3:
    9 = 1001
    3 = 0011

    so...

    1001
    + 0011
    ---------
    0001

    1 = 0001 in binary so 9 & 3 = 1
    think only with code.
    write only with source.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM