Sign in to follow this  
zlatko_bre_1985

pointer to function c++

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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();
*/

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I am realy sorry but i still cant make it work. :<
Here is what i tried

// test2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <vector>
#include <iostream>

class image
{
public:
image(){};
void Render();
};
void image::Render(){std::cout << "ASdfasdf\n";};

class anim
{
public:
anim();
std::vector <image *> aImages;
void (image::*pFunc)(void);
//void (std::vector<image *>::*pFunc)(); i tried with this decaration
//compiler accepts it but i still cannot set pFunc;
};
anim::anim()
{
aImages.push_back(new image());

//pFunc = aImages.at(0)->Render; //error C2475: 'image::Render' : forming a pointer-to-member requires explicit use of the address-of operator ('&') and a qualified name

//pFunc = anim::aImages.at(0)->Render; //error C2475:

//pFunc = anim::aImages::at(0)::Render; //'aImages' : is not a member of 'anim'
//'Render' : is not a member of 'operator``global namespace'''
//'Render' : undeclared identifier
//mising ; before render
//'at': identifier not found, even with argument-dependent lookup
//pFunc = anim::aImages[0]->Render; //error C2375
//pFunc = &anim::aImages[0]->Render; //error C2276: '&' : illegal operation on bound member function expression
//pFunc = &(anim::aImages[0]->Render); //error C2276
//pFunc = &(anim::aImages[0])->Render; //error C2276

//and here i lost all hope i think it needs something like (pointer to image)::*pFunc


};
anim::~anim()
{
delete aImages.at(0);
};

int _tmain(int argc, _TCHAR* argv[])
{
anim * animation;
animation = new anim();
// (animation->pFunc)();

delete animation;
return 0;
}


[\source]

Share this post


Link to post
Share on other sites

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