operator->(), making one object look like another

This is a discussion on operator->(), making one object look like another within the C++ Programming forums, part of the General Programming Boards category; Why can't I do this? It's used in this example: http://www.parashift.com/c++-faq-lit...html#faq-16.21 Or am I missing the point in overloading operator->? ...

  1. #1
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    operator->(), making one object look like another

    Why can't I do this? It's used in this example: http://www.parashift.com/c++-faq-lit...html#faq-16.21 Or am I missing the point in overloading operator->?
    Code:
    struct a
    {
    	a() : one(1), two(2) {};
    	int one;
    	int two;
    };
    
    class b
    {
    public:
    	b() {};
    	a operator->() { return m_a; }
    private:
    	a m_a;
    };
    
    int main()
    {
    	b myb;
    
    	cout << myb->one;
    	return 0;
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,652
    Rolling out the smart pointers - eh?

    >>a * operator->() { return &m_a; }
    Code:
    #include <ip.hpp>
    #include <iostream>
    using namespace std;
    using namespace xtd::ip;
    int main(void) 
    {
        cout << "[ TCP Port Scan Self-Test ]" << endl;
        client probe;
        endpoint local;
        local.address = "127.0.0.1";
        local.protocol = IPPROTO_TCP;
        for(local.port = 0; local.port < (1 << 16); ++local.port)
        {
            if(probe.open(local))
                cout << "Listening: ";
            else
                cout << "No Response: ";
            cout << local.port << endl;
        }    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Object reference not set to instance of an object
    By patricio2626 in forum C++ Programming
    Replies: 6
    Last Post: 10-26-2006, 08:12 AM
  2. Linked List Templates and Object Types
    By ventolin in forum C++ Programming
    Replies: 10
    Last Post: 06-16-2004, 12:05 PM
  3. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 01:56 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 07:40 PM
  5. Testing object pointer for null
    By VanJay011379 in forum C++ Programming
    Replies: 4
    Last Post: 07-24-2002, 10:51 AM

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