[C++] Chain of (function) pointers

Started by
2 comments, last by Decrius 15 years, 9 months ago
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.instance)->(pointers.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.instance)->(pointers.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]
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Advertisement
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.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)(); 



Quote:Original post by Decrius
The problem is this part:
((pointers.object.instance)->(pointers.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.object.instance)->(pointers.object.function)) == (Component *)(c->(f))


"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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.function on NULL, later on I either assign it with an adress, or I assign adresses to the pointers.object struct. So if pointers.function equals NULL, I assigned 'object.instance' and 'object.function' with pointers.
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora

This topic is closed to new replies.

Advertisement