Thread: Pointer to member typedef

  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413

    Pointer to member typedef

    Consider this code:
    Code:
    class X {
    	void foo() {}
    };
    
    typedef void (X::*foo)();
    
    void bar(foo n) {}
    
    int main() {}
    How in the world does the typedef bring foo into the global scope such that bar() can take an argument of type foo? What does the typedef really do here, and what really is the type of n as passed to bar()?
    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

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Correct me if I am wrong, but I believe what you have here are 2 different foo's in 2 different scopes. the first is the private member of X, the second is your typedef.

    Your typedef "foo" is "pointer to a member function of X, which returns a void and takes 0 arguments".

    X::foo() is still hidden from bar, and is unrelated to the typedef foo.
    Last edited by Bench82; 02-28-2006 at 08:57 AM.

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Code:
    class X {
    public:
    		void foo() {cout<<(void*)this<<endl;}
    	};
    
    typedef void (X::*haha)();
    void bar(haha n) 
    {}
    
    int main() 
    {}
    haha has type void(__thiscall X::*)(void)
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Code:
    void (X::*hop)() ;
    void *(X::foo)();
    Don't talk about bumping.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Thanks to Bench82 for the reply, I dont need any more help on this (which is why I didnt get back to the thread in the first place, but a thanks is in order nonetheless).
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-22-2008, 12:07 PM
  2. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  3. Pointer member not being recognised properly
    By drrngrvy in forum C++ Programming
    Replies: 9
    Last Post: 10-02-2006, 02:15 PM
  4. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  5. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM