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

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

    >>a * operator->() { return &m_a; }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(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, 02:56 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 07:40 PM