1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Pointer

Discussion in 'C++' started by inspiration, Jun 15, 2010.

  1. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    i!

    Contributions about pointer to class methods are indeed
    already in the forum. Still remain for me what this topic
    concern, a few questions unanswered. Would be nice if someone
    in answering the following questions might be helpful.

    Will ultimately produce for me exactly two of ambiguity:
    Problem 1:
    ----------

    I would firstly like to pointer to class methods in a
    LinkedList store.
    ie:
    the Konstruktorenaufruf could look like this:
    LinkedList <void (ClassName::*)(void)> list ();

    And now the crux:
    so I give this call as a template parameter pointer
    on methods of class 'Class Name' with the above structure to.

    This approach I might now, for example, compared with the Ablau, the
    would be required to create a LinkedList for 'ClassName' objects.
    also:

    a) LinkedList <ClassName *> list ();

    A call to like this would be also valid
    (I would only adjust the LinkedList internal):

    b) LinkedList <ClassName> list ();

    And here are my problems!

    ->
    If treatment of the LinkedList, is as done as you b below)
    also in the class methods possible?
    Is to say:
    I may in the call:
    LinkedList (ClassName::*)(void)> <void, where I indeed a PointerType
    specify, also specify a Klassenmethodentyp?
    ==> Looks like the syntax for then?

    Problem 2:
    ----------

    In various posts in the forum already mentioned that in a
    Calling a class method always binding to a specific object is required.
    ie:
    either as outside of the object like this: (object-> * object-> methodPtr) ();

    or within the object like this: (this-> this-*> methodPtr) ();

    In this approach, but is always assumed that the methods Pointer
    as an attribute of 'object' is present.

    What is it but now, if I belong to the object a pointer a
    specific class method is present!
    How do I call for then the class method?
    How do I bind because now the method of the object?

    Would be nice if someone could help on the jumps!
     
  2. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    OK!
    Item number two has done it!
    Only as far as problem number one, am
    I, unfortunately, still no further!

    Can someone help me also to this evil
    ? Eliminate
     
  3. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    So std:: list
    Code:
    # Include <list> 
    using namespace std; 
    
    class foo 
    ( 
    public: 
        void f1 () () 
        void f2 () () 
    ); 
    
    int main () 
    ( 
        typedef void (foo:: * mem_func_ptr) () / / not necessary but nice 
        list <mem_func_ptr> bla; 
        bla.push_back (& foo:: f1); 
        bla.push_back (& foo:: f2); 
    
        foo f; 
        for(list <mem_func_ptr>:: iterator i = bla.begin (); i! bla.end = (); + + i) 
            (F * (* i)) () 
    )
     
  4. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    And if he wants to manage method-pointer coming from other classes as foo?
     
  5. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    First of all thank you for your answers!

    But:
    Maybe I am so, what problem number one is concerned,
    expressed something unclear.

    Syntax of the form:

    typedef void (foo:: * mem_func_ptr) ();
    list <mem_func_ptr> bla;

    or:

    list <void (foo::*)()> bla;

    I know!

    However, I still give as a template parameter
    Methods-POINTER type to. I would be interested to know whether it
    also be a method type is indicated (no Ptr).
    In a normal class it works so well:

    <ClassName *> list list () or list <ClassName> list ()

    depending on what awaits the LinkedList.
    I also see, in the appropriate literature, only the syntax
    for method pointers,
    So: Return value (class name ::*)( parameters)
    but never, for a method type.

    Is there such a construct, or perhaps not
    is a fallacy in charge?

    Please hold on!
     
  6. meyup

    meyup New Member

    Joined:
    Feb 15, 2010
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    I do not think that would be yes, would you put the function in the list. In C + + functions, but are not objects, they can not copy or delete or something. To that extent, I can not imagine that they could put that species and container.
     
  7. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    what you are looking binder
    Code:
    # Include <list> 
    using namespace std; 
    
    class foo 
    ( 
    public: 
        void f1 () () 
        void f2 () () 
    
        typedef void (foo:: * mem_func_ptr) (); 
    ); 
    
    
    class bar 
    ( 
    public: 
        bar (foo * instance, foo:: mem_func_ptr func) 
            : Instanz_ (instance), 
                 func_ (func) 
        ( 
        ) 
    
        void operator() () 
        ( 
            (Insatnz_-> * func_) (); 
        ) 
    private: 
        foo * instanz_; 
        foo:: mem_func_ptr func_; 
    ); 
    
    int main () 
    ( 
    
        list <bar> bla; 
    
        foo a; 
        bla.push_back (bar (& a, & foo:: f1)); 
        
        foo b; 
        bla.push_back (bar (& b, & foo:: f2)); 
    
        for(list <bar>:: iterator i = bla.begin (); i! bla.end = (); + + i) 
            (I) (); 
    )
     
  8. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    Thank you!
    So that I would be happy for the moment.

    -> The idea to encapsulate methods and object pointers
    and only then can I insert in the LinkedList
    good use!

    But would certainly also ne good idea, the class 'bar'
    to write a template class and then one of these interface
    'SuperBar' derived. Then I could even methods and object pointers
    any class in 'bar' objects encapsulate and then, as' objects bar'
    indeed 'SuperBar' objects are, as' SuperBar' objects in the LinkedList
    list <SuperBar> bla insert.
    ==> Then I could add each item in the list of 'blah' as the command "execute () '
    and assign each 'bar' object could be the referenced method
    bind to the affected object and execute the method.
    We'll see if it's funtzt!

    Thank you for your help!
     
  9. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    well, that funzt so far, only then it will only work with pointers SuperBar

    list <SuperBar*> bla
     
  10. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    That's right!
    For the LinkedList, which I used in my code sample
    I have to specify a PtrTyp. Then I did not even care.

    But what about if I write the LinkedList itself. As my
    own list of template parameters interpreted certain Whether an object or
    Ptrtyp, I can not control, is not it?

    I think then the syntax should be correct!
     
  11. meyup

    meyup New Member

    Joined:
    Feb 15, 2010
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    So what? execute () can he do but at a pinch, and virtually redefined in cash
     

Share This Page