Thread: Ampersands

  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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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