Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

C++ lol

Name: Anonymous 2011-03-29 20:53

so how does one store a pointer to a member function and the object from which to call that function, and then call said function from said object?

example:

struct test {
 /*generic function object*/ fun_ptr;
 some_object* object_instance;
} this_test;

some_object new_obj;
this_test.fun_ptr = &some_object::some_function;
this_test.object_instance = &new_obj;

this_test.object_instance->this_test.fun_ptr(/*arguments*/);

???

Name: Anonymous 2011-03-29 20:54

C++ is a criple. Don't make fun of it.

Name: Anonymous 2011-03-29 20:56

>>1
push arg
mov ecx, obj
call fun

Name: Anonymous 2011-03-29 21:01

>>3
Yeah, I know. I'd rather do it in C++ as much as possible, though if all else fails...

Name: Anonymous 2011-03-29 21:33

>>4
I'm not exactly sure how boost::bind does it, but it can be used to create boost::thread objects that execute member functions.

Name: Anonymous 2011-03-29 21:42

>>5
Well, with boost I tried std::vector<auto> func_vector;
func_vector.push_back(boost::bind<rettype>(function, ptr_to_instance [implicit this], _1, _2... _n)); where n = last argument number to reserve, and then call that with:

func_vector[0](arguments); where arguments is the addresses of data I wish to fill, but that will fill said data with null, no matter what, while trying to call the bound function manually (ie out of the usual caller's class, for example in main) works.

I'll look into boost::thread, though, thanks.

Name: Anonymous 2011-03-29 21:43

std::function from tr1 or c++0x can be constructed with a member function pointer. The object isnt bound to it though, you need to pass it in as the first argument when invoking it. GCC, Msvc++, and other c++ compilers support it. Google for documentation.

Name: Anonymous 2011-03-29 21:45

Dont bother with boost.thread, c++0x just passed the final technical vote. Use std:: thread.

Name: Anonymous 2011-03-29 22:12

>>5
All I could find about boost::thread is that it is boost's implementation of threading. Pretty cool, but I can't see how I could use it to call an arbitrary member function on an arbitrary object of the corresponding class (not that I've ever used threads, though, this is something I'm exploring once the basics of my code are down.

>>7
Yes, with something like std::function<rettype(arguments)> my_func = std::mem_fn(&some_object::some_function) I assume.
But that is equivalent to the bound form, afaik, as boost::bind implicitly calls boost::mem_fn, which is the same as std::mem_fn for all intents and purposes; I have already tried calling my function with

boost::bind(this_test.fun_ptr, this_test.object_instance, _1, _2, _3)(arguments); but that gave me an error: does not evaluate as a function taking 4 arguments, presumably because this_test.object_instance isn't resolved as being of the same class as the function (therefore it isn't turned into an implicit this).
If it should behave differently, did I mess up somewhere? will function really work better there?

Name: Anonymous 2011-03-29 22:43

Use c++0x lamdas and closures to capture arguments and forward them to an inner function invocation. Its the proper c++0x way.

Name: Anonymous 2011-03-29 22:45

>>10
The proper any other sane language way?

Name: Anonymous 2011-03-29 23:10


class some_object
{
public:
    int some_function(int a, int b)
    {
        return a + b;
    }
};

template<typename ClassT, typename FuncT> struct test
{
    FuncT(ClassT::* fun_ptr);
    ClassT* object_instance;
};

    some_object new_obj;
    test<some_object, int(int, int)> this_test;

    this_test.object_instance = &new_obj;
    this_test.fun_ptr = &some_object::some_function;

    int ret = (*(this_test.object_instance).*this_test.fun_ptr)(1, 2);

Name: Anonymous 2011-03-29 23:20

>>10
As I understand it, this shouldn't work and is pretty much the first thing I have attempted.
I passed
[&new_obj](args){new_obj.some_function(args)} -> void
which is what I think you are suggesting I do, to the class (this_test in this example) that then tried to call the function. The class' function that calls the function given to it is something like:

do_stuff(std::tr1::function<void(args)> the_function){
 the_function(args); //where args are pointers to data to fill out
 if(!arg0) MyErrorReportTool::Report(LastError); //where arg0 is one of the arguments passed earlier.
}

the_function is provided by a list that stores like functions.

Needless to say (or I wouldn't be seeking help here), that always triggered (or something of the like... it didn't work anyway, something about being unable to resolve some_object, probably because do_stuff() is in a dll and the_function comes from an exe, different heaps, etc... not clear exactly on where what is allocated but that's my uneducated guess).

I'll retry that, though, since my code has somewhat changed in the meantime.

Name: Anonymous 2011-03-29 23:23


                            |\       
                            | |      
 Console.WriteLine("Yo!")   | |      
                            | |      
      \                     | |      
       \      ))))))))      | |      
        \   ((((    \       | |      
         \   \\\.=<#-<#     | |      
          \   \C     7      | |      
,              \    -)      | |      
\\__         __.) (.__      | |      
 \\\\ _    /'         `\    | |      
  \_ '/   /  ,       .  \   | |      
    \ \  /  /| '   ' |\  \  | |      
     \ \/  / |       | \  \ | |      
      \  /'  |       |  `\ \| |      
       `'    |       |    \ | |      
             |   .   |     \| |      
             >-------<      | |\     
            [~~~~~~~~~]     | | )    
            [    L    ]     | \/     
            [    |    ]     | |      
            [____|____]     | |      
             | /   \ |      | |      
             ()     ()      | |      
             ||     ||      | |-._   
             ||     ||      | |_  `. 
             )(     )(      | | `-. `.
            /==\   /==\     | |    `.;
           ooooO} {Ooooo    | |      `
           ~^^^~   ~^^^~    |/

Name: Anonymous 2011-03-29 23:27

>>12
I'll try that tomorrow. If that works as expected in my situation (which it probably does as far as I can tell), consider yourself the winnar.

Still open to any suggestions in the mean time, etc.

Name: Anonymous 2011-03-30 0:22

>>8
c++0x
All the problems of C++, plus 0x more

Name: Anonymous 2011-03-30 0:39

>>16
In base 36?

Name: Anonymous 2011-03-30 0:45

why not just call the member directly through the instance reference

Name: Anonymous 2011-03-30 1:47

>>18
I'd guess he's dealing with a C callback API that only lets him specify a single pointer parameter, and is using that as a pointer to a struct passed to a stub function that calls a class member function.

Name: BLACK HITLER 2011-03-30 3:57


    ░░░░░░░░░░░░░░░▄░░░░░░░░░░░░░░░
    ░░░░░░░░░░░░░▄▀█░░░░░░░░░░░░░░░
    ░░░░░░░░░░░▄▀░░█░░░░░░░░░░░░░░░
    ░░░░░░░░░▄▀░░▄▀░░░░░░░░░░░░░░░░
    ░░░░░░░░█▄░▄▀░░░░░░░░▄█▄░░░░░░░
    ░░░░░░░░█░▀▄░░░░░░░▄▀░█░▀▄░░░░░
    ░░░░░░░░▀▄░░▀▄░░░▄▀░░▄▀▄░░▀▄░░░
    ░▄░░░░░░░░▀▄░░▀▄▀░░▄▀░░░▀▄░░▀▄░
    ░█▀▄░░░░░░░░▀▄▀█▀▄▀░░░░░░░▀▄░█░
    ░█░░▀▄░░░░░▄▀░░█░░▀▄░░░░░░░░▀█░
    ░░▀▄░░▀▄░▄▀░░▄▀░▀▄░░▀▄░░░░░░░░░
    ░░░░▀▄░░█░░▄▀░░░░░▀▄░▄█░░░░░░░░
    ░░░░░░▀▄█▄▀░░░░░░░░▄▀░█░░░░░░░░
    ░░░░░░░░▀░░░░░░░░▄▀░░▄▀░░░░░░░░
    ░░░░░░░░░░░░░░░▄▀░░▄▀░░░░░░░░░░
    ░░░░░░░░░░░░░░░█░▄▀░░░░░░░░░░░░
    ░░░░░░░░░░░░░░░█▀░░░░░░░░░░░░░░
    ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
    ░█▄░█░█░▄▀▀▄░░█░░█░█▀▀░▀█▀░█░░░
    ░█░█▄░█░█░▄▄░░█▄▄█░█▄▄░░█░░█░░░
    ░█░░█░█░▀▄▄▀░░█░░█░█▄▄░▄█▄░█▄▄░
    ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

glory BLACK AFRIKA
HEIL NIGGERS.
HEIL BLACK AFRIKA.
NIG HEIL BLACK HITLER!

Name: Anonymous 2011-03-30 16:30

>>19
More or less. It's a C callback api that lets me pass one or two pointers: if one, it accepts a function pointer or a functor only. If two, it accepts a function pointer or a functor, and a pointer to the instance of the class on which I want to call the functor. The api won't know what function I want to call at compile time, so I can't just call the member function by providing the instance's address. Calling the function with a syntax such as pointer_to_object->functor() didn't work last time I tried. The compiler complained that functor() doesn't belong to object.

Name: Anonymous 2011-03-30 18:00

extern C int function(struct whatsit, void *what) { ((class)whatsit)->what() };

Name: Anonymous 2011-03-30 20:05

>>22
"error: whatsit has no member what", etc.

Name: Anonymous 2011-03-30 22:16

>>23
u srs?

Name: Anonymous 2011-03-30 22:34

>>24
Yup. That is because the argument passed as void* what is unresolved until after compile-time, so the compiler searches for a what member inside whatsit, which doesn't exist, instead of searching for the member represented by what.

Name: Anonymous 2011-04-01 17:08

Still not solved, by the way, and I'd like it to get solved sometime, if that wasn't obvious...

Name: Anonymous 2011-04-01 19:30

>>26
I don't like your tone.

Name: Anonymous 2011-04-01 19:40

>>27
I don't like your tuna.

Name: Anonymous 2011-04-01 20:09

>>28
It was the best fish butter.

Name: Anonymous 2011-04-01 21:01

/prog/ is dead

Name: Anonymous 2011-04-01 21:02

Long live /prog/!

Name: Anonymous 2011-04-01 21:04

Long live /prog/!

Name: Anonymous 2011-04-01 21:05

Long live /prog/!

Name: Anonymous 2011-04-01 21:07

/prog/ is dead

Name: Anonymous 2011-04-01 21:08

Long live /prog/!

Name: Anonymous 2011-04-01 21:10

/prog/ is dead

Name: Anonymous 2011-04-01 21:12

Long live /prog/!

Name: Anonymous 2011-04-01 21:13

Long live /prog/!

Name: Anonymous 2011-04-01 21:15

/prog/ is dead

Name: Anonymous 2011-04-01 21:16

/prog/ is dead

Name: Anonymous 2011-04-01 21:18

/prog/ is dead

Name: Anonymous 2011-04-01 21:20

Long live /prog/!

Name: Anonymous 2011-04-01 21:21

Long live /prog/!

Name: Anonymous 2011-04-01 21:23

/prog/ is dead

Name: Anonymous 2011-04-01 21:24

Long live /prog/!

Name: Anonymous 2011-04-01 21:26

Long live /prog/!

Name: Anonymous 2011-04-01 21:28

Long live /prog/!

Name: Anonymous 2011-04-01 21:29

Long live /prog/!

Name: Anonymous 2011-04-01 21:31

Long live /prog/!

Name: Anonymous 2011-04-01 21:32

Long live /prog/!

Name: Anonymous 2011-04-01 21:34

/prog/ is dead

Name: Anonymous 2011-04-01 21:36

Long live /prog/!

Name: Anonymous 2011-04-01 21:37

Long live /prog/!

Name: Anonymous 2011-04-01 21:39

Long live /prog/!

Name: Anonymous 2011-04-01 21:40

/prog/ is dead

Name: Anonymous 2011-04-01 21:42

Long live /prog/!

Name: Anonymous 2011-04-01 21:44

Long live /prog/!

Name: Anonymous 2011-04-02 8:04

/prog/ was always dead.

Name: Anonymous 2011-04-02 13:04

>>27
I was just saiyan

Don't change these.
Name: Email:
Entire Thread Thread List