Ampersands

This is a discussion on Ampersands within the C++ Programming forums, part of the General Programming Boards category; What are ampersands for when used with member declarations? I first noticed it here: [15] Input/output via <iostream> and <cstdio>, ...

  1. #1
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587

    Ampersands

    What are ampersands for when used with member declarations? I first noticed it here: [15] Input/output via <iostream> and <cstdio>, C++ FAQ
    Code:
     #include <iostream>
     
     class Fred {
     public:
       friend std::ostream& operator<< (std::ostream& o, Fred const& fred);
       ...
     private:
       int i_;    // Just for illustration
     };
     
     std::ostream& operator<< (std::ostream& o, Fred const& fred)
     {
       return o << fred.i_;
     }
     
     int main()
     {
       Fred f;
       std::cout << "My Fred object: " << f << "\n";
       ...
     }
    std:stream&, what's it for? And what's the dif in effect between pre and post fix.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    It specifies that the return type is a reference to a std::ostream.

    Quote Originally Posted by User Name:
    And what's the dif in effect between pre and post fix.
    In what context?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Code:
    template<class T>
    T &ByteArray::operator[](unsigned int i)
    {
    	return (T(bytes))[i];
    }

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    Well (T(bytes))[i++], (T(bytes))[++i], (T(bytes)[i]++, and ++(T(bytes))[i] are certainly all different. The difference lies both in what they increase and in what they return. Postfix increment will return the variable's value as it was, before the increment. In prefix, the variable is incremented and then return the value. Increments bind to the thing they are closest to, so some of the statements like i++ increase i, and the others increase the bytes element. Knowing the difference could save some little gray cells.

  5. #5
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    I'm aware of how pre/post increment works. I was asking about what the ampersand("&") does when pre/suf-fixed to a method, and what the difference, in effect, between prefix and suffix.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    There is no prefix and postfix for &, which are called references. They appear next to a type and specifies a reference.
    A reference is basically an alias for an existing variable. Sort of like pointers. Google references for more information.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There is no difference, just as there is no difference where you put the * with pointers.

    Code:
    char* foo();
    char *bar();
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To Webmaster...ampersands mis-interpreted
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-09-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21