Could someone explain this?

This is a discussion on Could someone explain this? within the C++ Programming forums, part of the General Programming Boards category; Can somebody explain why this prints 10 both times? Code: #include <iostream> using namespace std; class fred { int _i; ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Could someone explain this?

    Can somebody explain why this prints 10 both times?

    Code:
    #include <iostream>
    using namespace std;
    
    class fred {
            int     _i;
    public:
            fred (int v) { _i = v; }
            friend ostream &operator<< (ostream &o, const fred *f);
            friend ostream &operator<< (ostream &o, const fred &f);
    };
    
    ostream &operator<< (ostream &o, const fred *f)
    {
            return o << f->_i;
    }
    
    ostream &operator<< (ostream &o, const fred &f)
    {
            return o << f._i;
    }
    
    int main (int argc, char *argv[])
    {
            fred    *f = new fred (10);
            fred    f2 (20);
    
            cout << "f: " << f << endl;
            cout << "f2: " << f << endl;
    
            delete f;
            return 0;
    }
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Why should variable f output anything but 10?
    Sent from my iPadŽ

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by SlyMaelstrom
    Why should variable f output anything but 10?
    Why does outputting f2 also output 10 when I've passed 20 to the constructor?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Where do you output f2?
    Sent from my iPadŽ

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I see it now. Ahem.
    Jesus christ I'm blind.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you explain these bitwise operations?
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 10-29-2008, 01:19 PM
  2. One Easy" C " Question. Please Solve and Explain.
    By RahulDhanpat in forum C Programming
    Replies: 18
    Last Post: 03-24-2008, 01:39 PM
  3. Please Explain me few terms that i have listed in here.
    By chottachatri in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 07:20 AM
  4. Please explain?
    By neo_phyte in forum C Programming
    Replies: 3
    Last Post: 08-25-2006, 05:23 AM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM

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