Thread: Attaching functions to a class/struct

  1. #1
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001

    Attaching functions to a class/struct

    I'm doing a little experimenting with a COM-like structure but I'm not using COM per se.

    The goal is to be able to add functions to a class/struct or whatever dynamically at run-time.

    struct FuncExp
    void **Funcs;
    void Attach(void *Func);
    void Attach(void **FuncList,int numfuncs);

    Problem is to call these funcs I would have to create a special call function. I would rather call these functions just as you would in normal C code. But, the struct does not 'know' what functions it has nor does it know the prototypes for thoe functions. Essentially I must create my own vtable for this to work. Upon querying the object it should return a pointer to the interface for the object. Sort of like COM. I'm working on this for possible implementation in a new OS and since COM is copyrighted it would not serve to use it.

    The goal is to be able to dynamically attach and detach functions via pointers to and from objects. Perhaps I need to read a book on COM to see how it truly functions underneath the hood - using it and coding something like it are 2 completely diff things.

    And even though the GUID system works for COM I'm not a big fan of it so I would need a way to uniquely identify each object. I know these objects must be pre-compiled and sitting somehwhere for someone to use. As well there must be a mechanism for retrieving pointers to these objects - but this function can not be relative to the object being used. Very simple concept but very confusing.
    Last edited by VirtualAce; 08-04-2003 at 07:19 AM.

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Yeah something like that. I will take a look at your code much closer later (when I'm home) to disect it. Thanks a mil Salem - perhaps you've pointed me in the right direction.

    I did think of having a table and then searching it to see if the function is indeed part of the struct. As you can see the whole idea is rather complex and the code would be rather intricate and complicated.

    Coding it in assembly using structs might be an option but I don't want to make this code compiler/platform specific. It's really more of a binary standard than an actual C class or struct, but it is very much like a C class/struct. Confused yet?

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Yeah, I was thinking function pointers were the way to go. By doing it, the actual function being called can be interchanged, as long as they have the same signature. Could you could use a va_arg list for the parameters? Then, for the return value, you could create a struct that contains a char*/string with the type and then a void* that holds the data. Using typeid could help you out (assuming you're using C++).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM