Creating a Value object with a functionpointer as an argumen


#1

Hi Jules,
I tried to create Value obj with var::MethodFunction as an argument. But i am stuck with what argument to pass.
MethodFunction is expecting a function with const var as return type and 2 arguments and should be a method of DynamicObject class.
So how can i pass the address of my function as an argument.

class A
{
    const var fun(const var* aaa, int numArguments)
   {
   }
};

Value valueobj(var(&fun));

Please provide some example to use…

Thanks,
Noorul


#2

It’s a member function pointer, so the syntax would be &A::fun


#3

Thanks, it works…
Can you please clarify this doubt.
You have declared the function pointer as DynamicObject::*MethodFunction, doesn’t it mean that you can pass the address of member function belongs to only DynamicObject class…

I tried the same thing in the below code, it doesn’t work…

class A//Similar to DynamicObject
{
public:
};

class B//Similar to var
{
public:

	typedef void (A::*ptr2fn) (); 

	B(ptr2fn ddd)
	{
	}
};

class C//My class which has the method implemented
{
public:
	void fun()
	{
           //My code
	}
};

void main()
{
	B da(&C::fun);
}

Getting error as
error C2664: ‘B::B(B::ptr2fn)’ : cannot convert parameter 1 from ‘void (__thiscall C::* )(void)’ to ‘B::ptr2fn’

Please tell me if i am doing anything wrong…


#4

Yes, I guess you may have to explicitly cast it to a var::MethodFunction.


#5

If we are allowed to pass any class’s member function pointer, then instead of DynamicObject::*MethodFunction, can we declare as var::*MethodFunction or any class of JUCE::MethodFunction


#6

I think the class would need to be derived from DynamicObject, you could get into trouble just giving it random functions to use.


#7

When creating the object of var with pointer to function as an argument, it truncates that argument to bool and using the var(bool) constructor to create the object…
So please tell me how to create object with pointer to member function.

In my code, if i add another parameterised constructor with bool as an argument it is not giving error.


#8

There’s a DynamicObject::setMethod function for setting these things - best to use that.


#9

I am getting error while setting method for it.

[code]class A
{
public:
const var fun(const var* arguments, int numArguments)
{
return true;
}
};

DynamicObject* pobj = new DynamicObject();
pobj->setMethod(var::identifier((“Test”)), &A::fun);[/code]

error C2664: ‘juce::DynamicObject::setMethod’ : cannot convert parameter 2 from ‘const juce::var (__thiscall A::* )(const juce::var *,int)’ to ‘juce::var::MethodFunction’


#10

Like I said, do a cast:


#11

error C2440: ‘type cast’ : cannot convert from ‘const juce::var (__thiscall A::* )(const juce::var *,int)’ to 'juce::var::MethodFunction’
1> Pointers to members have different representations; cannot cast between them


#12

Well, in some actual code that I wrote, I used something like this:

[code]class MyObject : public DynamicObject
{
const var foo (const var* params, int numParams)
{
return var::null;
}

const var bar (const var* params, int numParams)
{
    ...
    return var::null;
}

MyObject()
{
    #define DECLARE_METHOD(name)  setMethod (#name, (var::MethodFunction) &MyObject::name)

    DECLARE_METHOD (foo);
    DECLARE_METHOD (bar);
    ...etc

};
[/code]


#13

I could not able to make it work.I think if we are declaring pointer to member function , we can access the member function of only that particular class, if we cast it is mentioned as undefined behavior in some forums…


#14

Well, it works just fine for me, with various different compilers. No idea what you’re doing differently.