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,699
    Rolling out the smart pointers - eh?

    >>a * operator->() { return &m_a; }
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

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