Thread: pointers !!!

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    pointers !!!

    Can any one please look at the below code (specifically the Command class and tell me what is the typedef void(Giant:: *Action)(); line doing and where is Action defined or declared?

    Code:
    class Giant
    {
      public:
        Giant()
        {
            m_id = s_next++;
        }
        void fee()
        {
            cout << m_id << "-fee  ";
        }
        void phi()
        {
            cout << m_id << "-phi  ";
        }
        void pheaux()
        {
            cout << m_id << "-pheaux  ";
        }
      private:
        int m_id;
        static int s_next;
    };
    int Giant::s_next = 0;
    
    class Command
    {
      public:
        typedef void(Giant:: *Action)();
        Command(Giant *object, Action method)
        {
            m_object = object;
            m_method = method;
        }
        void execute()
        {
            (m_object-> *m_method)();
        }
      private:
        Giant *m_object;
        Action m_method;
    };
    
    template <typename T> class Queue
    {
      public:
        Queue()
        {
            m_add = m_remove = 0;
        }
        void enque(T *c)
        {
            m_array[m_add] = c;
            m_add = (m_add + 1) % SIZE;
        }
        T *deque()
        {
            int temp = m_remove;
            m_remove = (m_remove + 1) % SIZE;
            return m_array[temp];
        }
      private:
        enum
        {
            SIZE = 8
        };
        T *m_array[SIZE];
        int m_add, m_remove;
    };
    
    int main()
    {
      Queue que;
      Command *input[] = 
      {
        new Command(new Giant, &Giant::fee), new Command(new Giant, &Giant::phi),
          new Command(new Giant, &Giant::pheaux), new Command(new Giant, &Giant
          ::fee), new Command(new Giant, &Giant::phi), new Command(new Giant,
          &Giant::pheaux)
      };
    
      for (int i = 0; i < 6; i++)
        que.enque(input[i]);
    
      for (int i = 0; i < 6; i++)
        que.deque()->execute();
      cout << '\n';
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    It's just a typedef for a member function pointer. For a 'flat' function, it would look like this:

    Code:
    typdef int ( * function_pointer_t )( int, float );
    
    int function( int lhs, float rhs )
    {    
          return 0;
    }
    
    int main( void )
    {
          function_pointer_t
                fpt = function;
          return fpt( 3114, exp( 1.0 ) );
    }
    For member functions, the syntax is just slightly more complicated to define, assign, and invoke one (eg: the example you posted).
    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;
    }

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    It's really a needless code, I wouldn't ever use something like that. It doesn't even look right. I don't even think it's used in this code heh. I don't even know what the typedef is called... Is it giant? no that's a class... Is it action? Maybe... Is it void? Can you even do that?
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by execute View Post
    It's really a needless code, I wouldn't ever use something like that. It doesn't even look right. I don't even think it's used in this code heh. I don't even know what the typedef is called... Is it giant? no that's a class... Is it action? Maybe... Is it void? Can you even do that?
    As pointed out, the typedef'ed name is Action, and it represents a pointer-to-member-of-Giant-function which returns void and has no arguments.

  5. #5
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    Quote Originally Posted by execute View Post
    It's really a needless code, I wouldn't ever use something like that.
    Perhaps you will perhaps you won't. While it may not be similar to the convention you more commonly seem to use, there may be times when you have to use these sort of convoluted types. The typedef simply makes the convoluted types that you so regretably have to use a little more asthetically pleasing. Clearly this is an implementation that requires such types. It all depends on what you are trying to do, and how you are attempting to do it.
    wipe on -
    A slap on the hand is better than a slap on the face. A tragic lesson learned far too late in life.
    - wipe off

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey guys..need help on pointers
    By Darkozuma in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2008, 02:57 PM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM