[C++] Pointer to function in a struct - crash [solved]

Started by
15 comments, last by Decrius 16 years ago
Quote:Original post by Decrius
Apparently it is.

I fixed the problem, though I want to add security for this.

How can I check if its a NULL pointer?

if (objects[o].items.function())
{
objects[o].items.function();
}

Doesn't seem to work =/

Thanks anyways :)
That checks if the return value of function() is 0 (NULL is almost always 0). You probably want: if (objects[o].items.function) (Note the lack of brackets).
Advertisement
If you know the code is irrelevant, you know what the problem is, since you can correctly remove irrelevant code. However, you don't know what the problem is, so you don't know what code is irrelevant.

Case in point: the problem was nowhere in the code you posted. Since you made us assume that o and i existed and were sane, there's no reason we should assume that the other parts of the code were wrong (e.g., that you initialized the function pointer). Once you bring assumptions into the mix, it's hard to get the crux of the problem efficiently.

Additionally, for example, if you are walking a pointer out of bounds, reading corrupt memory, or accessing a null pointer, the cause of that bug is almost never near the manifestation site. And all you've appeared to post in the manifestation site.

In the future, post your code without trying to guess at what is and is not relevant.
Quote:Original post by jpetrie
If you know the code is irrelevant, you know what the problem is, since you can correctly remove irrelevant code. However, you don't know what the problem is, so you don't know what code is irrelevant.

Case in point: the problem was nowhere in the code you posted. Since you made us assume that o and i existed and were sane, there's no reason we should assume that the other parts of the code were wrong (e.g., that you initialized the function pointer). Once you bring assumptions into the mix, it's hard to get the crux of the problem efficiently.

Additionally, for example, if you are walking a pointer out of bounds, reading corrupt memory, or accessing a null pointer, the cause of that bug is almost never near the manifestation site. And all you've appeared to post in the manifestation site.

In the future, post your code without trying to guess at what is and is not relevant.


Ok, sorry, will do.

@steve
Ah yes...looks very logic. Thanks.

[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
One other question. How can I point to a member function?

class Gui;class Graphics{    public:        void button();};void Graphics::button(){    game = true;    restatus();}class Gui{    struct Item    {        Rect rect;        bool active, over, click;        int rotate;        bool fit;        Texture texture, texture_over, texture_click;        void (Graphics::*function)();        Item() : rect(), active(false), over(false), click(false), rotate(0), fit(false), texture(), texture_over(), texture_click(), function(NULL) {}            };    struct Object    {        Rect rect;        bool active;        bool top;        std::vector<Item> items;        Object() : rect(), active(false), top(false) {}    };    public:        void load_prewindow(Graphics *, int, int, int, int, int);    private:        std::vector<Object> objects;};void Gui::load_prewindow(Graphics *graphics, int type, int x, int y, int w, int h){    objects[0].items[0].function = graphics->*button;    }


This tutorial didn't really work for me: http://www.newty.de/fpt/fpt.html
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
boost::function will allow you to create member function pointers.

In particular, boost::function and boost::bind:

#include <iostream>#include <boost/function.hpp>#include <boost/bind.hpp>class Test{	public:		void print()		{			std::cout << "Worked!" << std::endl;		}};int main(){	//instance of our class	Test test;	boost::function< void () > testFunction = boost::bind(&Test::print, &test);	testFunction();}
Tried that, getting this:

E:/Packs/Boost/include/boost/bind/mem_fn_template.hpp:40: error: cannot apply member pointer `((const boost::_mfi::mf0<void, Graphics>*)this)->boost::_mfi::mf0<void, Graphics>::f_' to `*boost::get_pointer [with T = Graphics*](u)', which is of non-aggregate type `Graphics*'E:/Packs/Boost/include/boost/bind/mem_fn_template.hpp:40: error: return-statement with a value, in function returning 'void'


class Graphics{        Graphics();        void button_game();};Graphics::Graphics(){    gui->load_prewindow(this);}void Graphics::button_game(){    game = true;    restatus();}class Gui{struct Item    {        boost::function< void () > function;    };struct Object    {        std::vector<Item> items;    };std::vector<Object> objects;public:int load_prewindow(Graphics *);};int Gui::load_prewindow(Graphics *graphics){objects[object].items[size].function = boost::bind(&Graphics::button_game, &graphics);}
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Nevermind, fixed.

I, ofcourse, had to remove the ampersand ;)

Thanks for your help, first time I use Boost ^^,
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora

This topic is closed to new replies.

Advertisement