Curious about function pointers and classes

This is a discussion on Curious about function pointers and classes within the C++ Programming forums, part of the General Programming Boards category; Is there a way to save a function pointer or a whole class (variables, members, methods and all) using C++ ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    14

    Curious about function pointers and classes

    Is there a way to save a function pointer or a whole class (variables, members, methods and all) using C++ or C. I was just wondering because I saw this interesting tutorial on this site about modular programming. I was wondering if it was possible to load a file that would hold a function or a class rather than a dll. I think this would allow the program to remain more platform independant.
    I saw a tutorial on the web that I found through google, but I can't seem to find it anywhere now --this was a while ago.


    Thanks in advance for any input offered.

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Sure, you can have a pointer to an object. But, this doesn't necessarily relate to modular programming.

    I did that when a function outside of the class needed to affect or access something inside the class... So, I passed a pointer to the object into the "external" function. (This may have been bad program design???) In my case the classes were in separete .CPP files, which I would consider modules.

    I don't have a formal definition, but I think of modular programming as separating and organizing source code into different .CPP (and .H) files. These files can be compiled together into one .EXE file. This makes the modules reusable (at compile time) in another program. Here's the multiple source-file FAQ.

    You can write a library, or use a 3rd-party library. I would consider that modular programming too.

    Compiling parts of the code to separate DLLs also makes them reusable (at runtime) allowing various programs to share the DLL part of the code.
    Last edited by DougDbug; 06-29-2005 at 03:45 PM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    14
    Not quite what I am looking for this guy programed a game engine that loads DLLs at run-time. Kind of like working with plugins what I am trying to figure out is if there is a way to do this without using DLLs. The tutorial I saw via Google somebody had save an actual function to a files and called it like it was an EXE. Somebody once told me that C/C++ was powerful enough to build itself and was just curious as to how would one go about doing this. As I was writing this I just realized something classes have methods but so do structures(or at least they can I think)... You can save a structure to a file... and call a structure into a program. Hmmmmmm..... Well there is only one way to find out for sure. TTFN I'll post the results if it works.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Location
    Stockholm, Sweden
    Posts
    64
    Why would you want to do it without libraries?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    14
    Library must be used when compiling they are hard coded into the program. It would be easier to upgrade, edit a porgram by just replacing a file or a DLL unfortunately DLLs aren't platform independant.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Location
    Stockholm, Sweden
    Posts
    64
    The DLL format specification isn't platform independent, but the use of libraries is. If you provide the source code for your libraries an end-user could compile that source code on their machine. It doesn't matter if the libraries are compiled to DLL files in a Windows-environment or to .so files in a *nix-environment - the same goal is achieved.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    14
    But can libraries load at run-time? That is what I am really after

  8. #8
    Registered User
    Join Date
    Jun 2005
    Location
    Stockholm, Sweden
    Posts
    64
    Yes. A quick example that I can think of (off the top of my head) is the X-Chat IRC client.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    14
    Oh this may be something I will want look into. Do you know any good references on the subject?

  10. #10
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Somebody once told me that C/C++ was powerful enough to build itself
    Eeek! I suppose that could be done, but it would require that the user have the exact same compiler as you. There would also be a compile delay each time the user runs the program.

    Actually, this is something like the way JAVA works… and it reminds me of the early PC days when BASIC was run on an interpreter. In order to run a BASIC program you had to run BASIC first.

    a game engine that loads DLLs at run-time.... I am trying to figure out is if there is a way to do this without using DLLs.
    You want to "embed" these existing game-engine DLLs in your EXE file??? That might actually be possible because a DLL is mostly machine code... But, I think it would take some really serious black magic and hacking. If you have the source code for the DLLs, you can get by without the black magic.

    But can libraries load at run-time? That is what I am really after
    Ummm... 'Sounds like a DLL to me!


    About DLLs - Say you write 3 game programs, and they use a lot of common code for background image generation, motion... whatever... The programs might be 100MB each (300MB total) if they are written as stand-alone (statically linked) programs. Or, maybe each program can be 50MB plus 50MB of shared DLLs (200MB total).

    Windows comes with about a billion... Ok.. about 1000 DLLs. Windows programs use these DLLs, which makes all Windows programs "look alike", and the programs are much smaller than they would be if all of the graphics / GUI stuff was coded into each EXE.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    14
    I guess your right it would be a Java-ish approach with that I might as well embed java classes into my program or python mods I would run a little slower but the plugins can be shared between platforms. Not to mention I won't have write a parser.

    [indent]I don't want to use DLLs because they are not platform independant so users will be forced to compile an .so or .dll. That a user who has a linux version can share plugins painlessly with a user who has a win32 version.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Q: Pointers to Classes '->' notation
    By Howie17 in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2008, 09:09 AM
  2. Function pointers in C++ classes
    By eple in forum C++ Programming
    Replies: 8
    Last Post: 05-06-2004, 03:54 PM
  3. returning pointers from a function
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 10:37 PM
  4. Function pointers with classes
    By nickname_changed in forum C++ Programming
    Replies: 5
    Last Post: 10-01-2003, 10:19 PM
  5. function pointers in c++ classes
    By optimism_ in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2002, 02:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21