Question about function pointing

This is a discussion on Question about function pointing within the C++ Programming forums, part of the General Programming Boards category; I have a function set up so that it receives a function pointer, and that works: Code: #include<iostream> //----------------- //--My ...

  1. #1
    tol
    tol is offline
    Registered User
    Join Date
    Jul 2005
    Posts
    23

    Question about function pointing

    I have a function set up so that it receives a function pointer, and that works:

    Code:
    #include<iostream>
    //-----------------
    //--My NODE class--
    //-----------------
    class Node 
    {
     private:
     static Node * root;static Node * index;Node * next;
     char content;
     public:
     Node(char content)
     {
      if (root == NULL)
      {
       root = this;
       next = NULL;
      }
      else
      {
       Node * finger = root;
       while(!(finger->next==NULL)){finger=finger->next;}
       finger->next=this;
       this->next=NULL;
      }
      this->content = content;
     }
     static void Create(char content)
     {
      Node * newNode; newNode=new Node(content);
     }
     static int Count(void)
     {
      Node * finger; int count=0;
      finger=root;
      while(!(finger==NULL)){finger=finger->next;++count;};
      return count;
     }
     static void Step(void)
     {
      if(index==NULL){index=root;}
      else{index=index->next;}
     }
     static char Return(void) 
     {
      return index->content;
     }
      static void StepLoop(void (*Stepper)(void))
     {
      for(int x=0;x<Node::Count();x++){Stepper();};
     }
    };
    Node::Node * Node::root = NULL;
    Node::Node * Node::index = NULL;
    
    // End of Node class code
    void test(void)
    {
         Node::Step();printf("%c",Node::Return());
    }
    
    int main(void)
    {
    
     Node::Create('p');Node::Create('p');Node::Create('p');Node::Create('p');
     printf("%i\n",Node::Count());
     Node::StepLoop(test);
     printf("\n");
     system("PAUSE");
    }
    However, I would really rather be able to call
    Code:
    Node::StepLoop({Node::Step();printf("%c",Node::Return());});
    So that I could use StepLoop almost like a FOR block, but specialized for my objects. Is there any way to create a pointer to an anonymous block of code between {} without formally making it into a function?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    A pointer is a variable which stores an address in memory. As far as I know, there is no way to obtain the address of an anonymous block of code and store it in a pointer.
    Last edited by 7stud; 07-24-2005 at 01:03 PM.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    All subsequent replies have been deleted as they dont help the thread.

    If you want to answer - be constructive, not abusive

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Is there any way to create a pointer to an anonymous block of code between {} without formally making it into a function?
    No. At least not through C++. Perhaps trhough inline ASM, but I don't recommend inline ASM for this purpose.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    [mod] snip..snip [/mod]

  6. #6
    tol
    tol is offline
    Registered User
    Join Date
    Jul 2005
    Posts
    23
    Quote Originally Posted by Zach L.
    No. At least not through C++. Perhaps trhough inline ASM, but I don't recommend inline ASM for this purpose.

    Cheers
    You know, that has always made me want to try ASM. Most people tell me not to bother with ASM, but it occurs to me that since ASM is nearly 1-to-1 with machine code, you could actually move around INSTRUCTIONS as if they were simple values. It is not hard to see that this could have a few upsides

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Quote Originally Posted by Fordy
    [mod] snip..snip [/mod]
    Eh?

    At any rate, with regards to ASM, I've not done much with it myself (if Bubba is around, I know he has), but I would be hesitant to mix it with the higher level languages like C/C++, simply because this seems to defeat the purpose of the higher level language... Now, as for learning ASM for its own sake, I wish you luck if you try.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM

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