pointer to function c++

Started by
9 comments, last by zlatko_bre_1985 18 years, 9 months ago
I have a problem with pointer to function. I have class and i want to have a pointer to function in that class as its member.here is simplified look of the class. class Image { public: void Render (void); //i tried with __cdecl but nothing void (*pFunc) (void); } void Image::Image { pFunc = Render; }; and this is the error : 1) with void __cdecl Render(void ); error C2440: '=' : cannot convert from 'void (__cdecl Image::* )(void)' to 'void (__cdecl *)(void)' 2)with void Render (void) error C2440: '=' : cannot convert from 'void (__thiscall Image::* )(void)' to 'void (__cdecl *)(void)' Any help?
Serbia,Zrenjanin
Advertisement
pointer to functions and pointer to member functions are not the same and the syntax is not the same. So you cant assign a NON static member function to a plain pointer to function, also inorder to use a pointer to member function you need an instance of the type in question.
void (Image::*pFunc)(void);


Function Pointer tutorials

If you're trying to do what I think you are, you might want to look into using generic functors (see tutorial above) or look into something like boost::function.
Hi. Why not do something like this for what you already have:

class CImage
{
private:

CImage * _pimage;

public:


void Render();
void Update();

};

within the implementation file if you wanna call render do:

_pimage->Render();
Thanks it works.Good article and is the boost library reliable and is it comptabile with std?

And i have 3 levels down function so it would be inefficient if i could call
sprite->animations->image->render
so ill yust create image pointer to his render func,
and animation has bool imagechanged and when image (in anim) change set thet to true
sprite calling render first check anim->imagechanged and if true it updates his pointer.
Serbia,Zrenjanin
Quote:Original post by zlatko_bre_1985
Thanks it works.Good article and is the boost library reliable and is it comptabile with std?


Yes it is. In fact, large parts of boost (including boost::function) are already part of the Libraty TR1 (technical report 1), the official amendment to the standard library which will almost certainly be included in the next C++ standard.
COOL then ill start using boost library soon just to understand this stuff a little more.

I am in problems again.
Can i somehow make a pointer to function from another class.

My prob is this
Image class has a Render function.
Animation class has a std::vector<Image *> Images // which it dynamicaly populates
Animation has a AnimationSequence[],CurrentImage,

1)
How should i declare pCurrentImageRender
so i can use something like this
pCurrentImageRender = Images.at(currentImage)->Render;
2)How i call the func using pointer ??
/*
i tried this way :
a)make in Image void (Image::*iRender)(void)
b)In Image constructor i assign iRender = Render;
c)In Animation i made void (Image::*aRender)(void)
d)assign aRender = Images.at(currentImage)->iRender;
f)if in animation::Render i put aRender() it compiletime error says about func does not take 0 arguments
g)if in animation::Render i put void aRender() it just skip it over and does not call image::Render();
*/
Serbia,Zrenjanin
Something like that?

class Image{public:   void (Image::*iRender)();public:   Image() :      iRender(&Render)   {}private:   virtual void Render();};int main(){   Image* pImage = new Image;   (pImage->*(pImage->iRender))();}


Honestly, you might be better off with something like this:

class Image{private:   void (Image::*iRender)();public:   Image() :      iRender(&DoRender)   {}   void Render()   {      (this->*iRender)();   }private:   virtual void DoRender();};int main(){   Image* pImage = new Image;   pImage->Render();}


But, well, I don't know. What are you precisely trying to achieve?

And yes, Boost is perfectly OK.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by zlatko_bre_1985
And i have 3 levels down function so it would be inefficient if i could call
sprite->animations->image->render


Remember, something is not inefficient until you measured it to be inefficient! Write elegant code first and optimise later. You can use abstract base class (interfaces)to mimic function pointers (have a IRender class which has a virtual void Render()). Or you can also use functors as someone already establish.

Shadx

[Edited by - Shadx on July 11, 2005 11:19:11 AM]
ll now try what frunny told me and
calling
Sprite->Animation.at(sprite->currentAnim)->
images.at(Sprite->Animation.at(sprite->currentAnim)->currentImage)->
->render();

100% is the not efficient way.I think that etting the pointer to render() somehow into the sprite class and then calling that function is most efficient.Not to mention array bound checking for .at(xxx) and multiple parameter coping.
Serbia,Zrenjanin

This topic is closed to new replies.

Advertisement