Sign in to follow this  

[C++] Chain of (function) pointers

This topic is 3457 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Somehow, whatever I try (which is rather random at this point), I never get it right. Consider the following code:
    class A
    {
    public:

        void disconnect(Slot *c, void (Slot::*f)(Component *)) // pointer to class, pointer to memberfunction with parameter (Component *)
        {
            for (int i = 0; i < (int) pointers.size(); ++i)
            {
                if (((pointers[i].instance)->(pointers[i].function))(Component *) == (c->(f))(Component *))
                {
                    // function pointers point to the same function
                }
            }
        }

    private:
        struct Pointer
        {
            Slot *instance;
            void (Slot::*function)(Component *);
        };

        std::vector<Pointer> pointers;
    };


The problem is this part:
((pointers[i].instance)->(pointers[i].function))(Component *) == (c->(f))(Component *)
Where I try to compare if the pointer that is given through the function parameters, is the same as the pointer I test in the vector. It keeps giving me errors like these:
error: expected unqualified-id before '(' token
error: expected primary-expression before '*' token
error: expected primary-expression before ')' token
error: expected unqualified-id before '(' token
error: expected primary-expression before '*' token
error: expected primary-expression before ')' token
And I've come to a point where I'm absolutly clueless...even worse, I had it working before, didn't make a backup or used SVN yet '-.- If there is a more elegant way of checking if a certain value is in a vector, please do tell me ;) [Edited by - Decrius on July 30, 2008 5:18:30 PM]

Share this post


Link to post
Share on other sites
What is it that you are trying to do?

Are the definitions of Slot and Component known?

(Why do you cast the return value of vector::size() rather than use an unsigned type for i in the first place?)


if (!pointers[i].function && ...)

Are you trying to call a function only if the function pointer is NULL?

Is Component* supposed to be an argument for a function (you pass values, not types)?

Are you trying to compare the return values of functions that don't return anything?

Calling pointer to member function uses the .* and ->* operators, looking something like:

(pinstance->*pmem_fun)();



Share this post


Link to post
Share on other sites
Quote:
Original post by Decrius
The problem is this part:

((pointers[i].object.instance)->(pointers[i].object.function))(Component *) == (c->(f))(Component *)



Why do you have the typecast following each item rather than preceding it?

Have you tried putting the typecast in front, like so:


(Component *)((pointers[i].object.instance)->(pointers[i].object.function)) == (Component *)(c->(f))


Share this post


Link to post
Share on other sites
Sorry, should've put in more information.

Yes, I get an object pointer passed which derived from Slot. Also do I get a pointer of a function inside the derived class, with the argument (Component *).

So I'm not casting here, but rather using function pointers with (Component *) as parameter. That's why it's preceding.

If I don't cast the size(), MingW gives me warnings, so I just did it...

I'm comparing 2 function pointers, to see if they're equal, so if they point to the same function.

On initialisation, I put pointers[i].function on NULL, later on I either assign it with an adress, or I assign adresses to the pointers[i].object struct. So if pointers[i].function equals NULL, I assigned 'object.instance' and 'object.function' with pointers.

Share this post


Link to post
Share on other sites

This topic is 3457 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this