POV-Ray : Newsgroups : povray.off-topic : Another C++ question : Another C++ question Server Time
29 Sep 2024 17:20:31 EDT (-0400)
  Another C++ question  
From: Darren New
Date: 8 Mar 2009 13:39:30
Message: <49b402d2$1@news.povray.org>
On loading classes from a DLL...

I'm assuming if you want to use a different class at runtime depending on 
something a user types in (for example), what you need to do is...

1) Declare class Alpha with a bunch of virtual functions to do what you want 
in the main program.

2) Declare class Beta that inherits from Alpha in the DLL, implementing 
those virtual functions appropriately.

3) Declare a global function that returns pointers/references to instances 
of Beta in the DLL.

[repeat 2 and 3 for each class you might want to use as a child of Alpha]

4) Declare some sort of forward reference to the global function in (3) that 
gets filled in by loading the DLL (i.e., the dynamic linking part).

At runtime, load the correct DLL based on what the user typed, then invoke 
the global function and assign the result to a reference/pointer to Alpha.

Is that how you'd do dynamic code loading in C++? Or is there a better way? 
In particular, I'm assuming any code that actually instantiates a Beta is 
going to have to be in the same DLL that defines Beta, and that the main 
program can't actually refer to Beta if the main program gets compiled 
before the DLL gets written?

I'm thinking things like (say) editor macros, or "plug ins", or "stored 
procedures", or something like that, where you'd want to be able to load 
code at runtime that was written after the main routine was compiled.

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.