über pointer2member eine virtuelle funktion aufrufen



  • hi,
    wie kann ich über einen pointer2member eine virtuelle funktion aufrufen? brauch ich da paar tricks, denn virtual sagt ja erst zur runtime welche funktion aufgerufen wird...

    cu



  • Hallo,
    da sind keine Tricks notwendig, der Pointer ist in diesem Fall kein echter Pointer, sondern ein Datentyp, der alle notwendigen Informationen enthält:

    class Base {
    public:
    	virtual void f() {
    		cout << "Base::f" << endl;
    	}
    };
    
    class  Derived : public Base {
    public:
    	void f() {
    		cout << "Derived::f" << endl;
    	}
    
    };
    
    int main(){
    	void (Base::*pm)() = &Base::f;
    	Derived d;
    	Base& b = d;
    	(b.*pm)();	// Derived::f
    	(d.*pm)();  // Derived::f
    }
    


  • interessant;)
    könnte man da auch was mit boost::function machen?



  • boost::bind(boost::mem_fun(&Base::f), object);

    Dann hast du ein Functor, wie man den in boost::function bekommt weiß ich nicht (noch nie gemacht).



  • îch will doch keinen functor haben! nur so ne art funktionspointer mit boost::function...



  • boost::function f(boost::bind(&Klasse::Memberfunktion, pObject));
    


  • geht das ohne boost::bind nicht? so ca.:

    class foo 
    { 
    public:
    	virtual void bar(int x)
    	{
    		cout << "foo::bar " << x << endl;
    	}
    }; 
    
    class quu : foo 
    { 
    public:
    	void bar(int x)
    	{
    		cout << "quu::bar " << x << endl;
    	}
    };
    
    int main()
    {
    	boost::function2<void, foo*, int> f;
    	f = &foo::bar;
    	quu x;
    	f(&x, 5);
            cin.get();
    

    error C2243: 'type cast' : conversion from 'quu *__w64 ' to 'foo *' exists, but is inaccessible



  • quu muss öffentlich von foo erben, damit quu implizit nach foo konvertierbar. Du erbst in deinem Beispiel aber privat.



  • hm, upps;/
    weisst du wie boost::function intern funktioniert?

    ist das hier die einzige erkläreung wie das in etwas intern abläuft?
    In Boost.Function, an alternative but equivalent approach was taken using free functions instead of virtual functions. The Boost.Function object essentially holds two pointers to make a valid target call: a void pointer to the function object it contains and a void pointer to an "invoker" that can call the function object, given the function pointer. This invoker function performs the argument and return value conversions Boost.Function provides. A third pointer points to a free function called the "manager", which handles the cloning and destruction of function objects. The scheme is typesafe because the only functions that actually handle the function object, the invoker and the manager, are instantiated given the type of the function object, so they can safely cast the incoming void pointer (the function object pointer) to the appropriate type.

    cu



  • Steht da doch, so wie man eben member-function-pointer aufruft mit dem .* und dem ->* operator (ein beispiel sieht du ja weiter oben im thread von hume).


Anmelden zum Antworten