Overloading of functions in C++ code when using extern "C" - c++

I want to load libraries at run time. I have a base class "Base" and two derived classes "Derived1" and "Derived2" which should be loaded at runtime. I am using the approach from this answer with some small modifications. The code compiles perfectly when I define only one derived class, but it fails to compile when several derived classes are defined. The compilation error is the following:
multiple definitions of 'create'
I think that the problem is "C" doesn't allow overloading of function names. How would one solve this problem?
The important point is that since I don't know how many .so files exist, I want to have one handler for all .so files. The details of the code are given below:
base.hpp:
class Base {
public:
virtual ~Base() {}
virtual void foo() const = 0;
};
using Base_creator_t = Base *(*)();
derived1.hpp:
#include "Interface.hpp"
class Derived1: public Base {
public:
void foo() const override {}
};
extern "C" {
Base * create() {
return new Derived1;
}
}
derived2.hpp:
#include "Interface.hpp"
class Derived2: public Base {
public:
void foo() const override {}
};
extern "C" {
Base * create() {
return new Derived2;
}
}
Dynamic shared library handler: Derived_factory.hpp:
#include <dlfcn.h>
class Derived_factory {
public:
Derived_factory(char* path) {
handler = dlopen(path, RTLD_NOW);
if (! handler) {
throw std::runtime_error(dlerror());
}
Reset_dlerror();
creator = reinterpret_cast<Base_creator_t>(dlsym(handler, "create"));
Check_dlerror();
}
std::unique_ptr<Base> create() const {
return std::unique_ptr<Base>(creator());
}
~Derived_factory() {
if (handler) {
dlclose(handler);
}
}
private:
void * handler = nullptr;
Base_creator_t creator = nullptr;
static void Reset_dlerror() {
dlerror();
}
static void Check_dlerror() {
const char * dlsym_error = dlerror();
if (dlsym_error) {
throw std::runtime_error(dlsym_error);
}
}
};
main.cpp:
#include "Derived_factory.hpp"
int main(){
Derived_factory factoryOne("Derived1.so");
std::unique_ptr<Base> baseOne = factoryOne.create();
baseOne->foo();
Derived_factory factoryTwo("Derived2.so");
std::unique_ptr<Base> baseTwo = factoryTwo.create();
baseTwo->foo();
return 0;
}

The problem is not extern "C". The problem is you have multiple definitions of the same function.
Base * create() is indistinguishable from Base * create()

What you're trying to do is to have the same function in two different loadable modules. But what you're doing instead is putting both implementations (with the same name and signature!) of this function into the main module, which of course results in multiple definition error.
What you should do instead is put the create functions into the *.cpp files, i.e. derived1.cpp and derived2.cpp, omit their definitions from the *.hpp files, and compile the shared objects from these *.cpp files. I've modified your project to achieve this, see the live demo.

Related

How to prevent undefined references to internal functions of my API

I have created a C++ module which has an API part and an internal part. The API contains a long (more then 30) list of polymorphic classes inheriting directly or indirectly from the same baseclass. In my internal classes/functions these classes are used differently, so I moved some functionality to these classes to be used polimorphically. These functionalites depend on some internal resources, while the internal functions depend on these classes so I broke this circular dependency by forward declaring the necessary classes in the .h files and including them in the .cpp files. This way my module compiles and works perfectly.
Here comes the problem. When I include the API from another module, I get "undefined reference to ...." errors to internal functions in the API classes polimorphic functions. I think this is due to the forward declarations. These internal classes are never defined in the eyes of the second module which uses the API.
Broadly my module looks like this:
InternalClass.h:
class ApiObjBase;
class InternalClass {
public:
static InternalClass& get(); // singleton
void processApiObjBase(ApiObjBase& apiObj);
static const Resource& getResource1();
static const Resource& getResource2();
static const std::map<uint64_t, std::unique_ptr<ApiObjBase>>& getApiObjStorage();
UacManager(const UacManager&) = delete; // singleton
UacManager& operator=(const UacManager&) = delete; // singleton
private:
InternalClass(); // singleton
Resource resource1;
Resource resource2;
std::map<uint64_t, std::unique_ptr<ApiObjBase>> ApiObjStorage;
}
InternalClass.cpp:
#include "ApiObjBase.h"
void InternalClass::processApiObjBase(ApiObjBase& apiObj) {
apiObj.internalPolyFunc1();
apiObj.internalPolyFunc2();
// ...
}
const Resource& InternalClass::getResource1() {
return get().resource1;
}
const Resource& InternalClass::getResource2() {
return get().resource2;
}
// Other implementations of member functions ...
ApiObjBase.h:
class InternalClass;
class ApiObjBase {
protected:
int internalVariable1;
int internalVariable2;
int apiVariable1;
int apiVariable2;
public:
ApiObjBase() = default;
// getters, setters
void internalPolyFunc1() = 0;
void internalPolyFunc2() = 0;
void apiPolyFunc1() = 0;
void apiPolyFunc2() = 0;
}
ApiObjBase.cpp:
#include "ApiObjBase.h"
// ...
ApiObjDerived1.h:
#include "ApiObjBase.h"
class ApiObjDerived1 : public ApiObjBase {
protected:
int internalVariable3;
int internalVariable4;
int apiVariable3;
int apiVariable4;
public:
ApiObjDerived1() = default;
// getters, setters
void internalPolyFunc1() override;
void internalPolyFunc2() override;
void apiPolyFunc1() override;
void apiPolyFunc2() override;
}
ApiObjDerived1.cpp:
#include "ApiObjDerived1.h"
#include "InternalClass.h"
void ApiObjDerived1::internalPolyFunc1() {
Resource& res = InternalClass::getResource1(); //
// ...
}
void ApiObjDerived1::internalPolyFunc2() {
Resource& res = InternalClass::getResource1();
// ...
}
void ApiObjDerived1::apiPolyFunc1() {
// ...
}
void ApiObjDerived1::apiPolyFunc2() {
// ...
}
// ...
ApiObjDerived2.h:
#include "ApiObjBase.h"
class ApiObjDerived2 : public ApiObjBase {
protected:
int internalVariable5;
int internalVariable6;
int apiVariable5;
int apiVariable6;
public:
ApiObjDerived2() = default;
// getters, setters
void internalPolyFunc1() override;
void internalPolyFunc2() override;
void apiPolyFunc1() override;
void apiPolyFunc2() override;
}
ApiObjDerived2.cpp:
#include "ApiObjDerived2.h"
#include "InternalClass.h"
void ApiObjDerived2::internalPolyFunc1() {
Resource& res = InternalClass::getResource2();
// ...
}
void ApiObjDerived2::internalPolyFunc2() {
// ...
}
void ApiObjDerived2::apiPolyFunc1() {
// ...
}
void ApiObjDerived2::apiPolyFunc2() {
// ...
}
// ...
SomeOtherModule.cpp:
#include "ApiObjDerived2.h"
void main(int argc, char** argv) {
std::unique_ptr<ApiObjBase> apiObj = std::make_unique<ApiObjDerived2>();
apiObj.get()->apiPolyFunc1();
return EXIT_SUCCESS;
}
For this example the error would the following while linking SomeOtherModule:
(...) In function `ApiObjDerived2::internalPolyFunc1()':
(...) undefined reference to `InternalClass::getResource2()'
My only idea to this problem is to create wrapper classes (ApiObjBaseWrapper, ApiObjDerived1Wrapper, ApiObjDerived2Wrapper) for each polymorphic class and move all internal member variables and functions to them. These wrapper classes would contain the original derived class as a private member variable, which could be accessed by a virtual getter which returns a reference or a pointer to ApiObjBase. This would separate the internal and API part of the module making predeclarations unnecessary in the API, while maintaining all the advantages of polymorphism. After this, InternalClass could use ApiObjBaseWrapper, and SomeOtherModule could continue to use ApiObjBase.
My problem with this idea is that this would require a LOT of new files, classes and code (I would have to create another ~30 classes with .cpp and .h files). I cannot emphasize more how big this module is.
What do you suggest, what should I do?

throw undefined symbol when load dynamic lib with dlopen

I am having main program which has one base class that needs to be used by shared library.
Base class has some pure virtual method that derived class in shared library needs to be over written.
Main program load shared library using dlopen system call.
dlopen("shared file name", RTLD_NOW|RTLD_GLOBAL);
Base class
class RateComputer
{
public:
RateComputer();
virtual ~RateComputer();
virtual void OnMarketData() = default;
private:
};
Derived class in shared library.
class WeightedRateComputer : public RateComputer
{
public:
WeightedRateComputer();
~WeightedRateComputer();
void OnMarketData() override;
private:
};
and implement
WeightedRateComputer::WeightedRateComputer()
{
printf("in Construct\n");
}
void WeightedRateComputer::OnMarketData()
{
printf("in OnMarketData\n");
}
extern "C" WeightedRateComputer* createObj()
{
return new WeightedRateComputer();
}
While compilation of binary and so file I have added -rdynamic flag. But during loading of library with dlopen it gives error "undefined symbol: _ZTI12RateComputer".
int main()
{
void *handle = dlopen("../xx.so", RTLD_LAZY |RTLD_GLOBAL);
if (handle == NULL)
{
printf("%s\n", dlerror()); //throw error here
return 0;
}
return 0;
}
As Constructor and Destructor are declared in your RateComputer class, they need to be defined.
At least, you can use default c++11 keyword to use default implementation.
Moreover, as your RateComputer class is an interface, your OnMarketData method should be pure virtual, hence only declared in the interface and implemented in derived classes.
Here's the RateComputer class code I end up with :
class RateComputer
{
public:
RateComputer() = default;
virtual ~RateComputer() = default;
virtual void OnMarketData() = 0;
};
Hope this helps.

Inheritance in C++

EDIT 2:
Here is a simple summary of what I want to do (I think):
I want to dynamically create global instances based on conditions that are calculated at run time.
You can skip to EDIT1 if you'd like to take a look at sample code, but at this point, the above bolded-text is probably the easiest to understand...
END EDIT 2.
My question is about polymorphism and inheritance. Specifically, I want to know if there is a way I could inherit functions and pointers from another class.
I have a class called Globals which contains various pointers to objects to other classes as well as various functions. Instead of copy/pasting code, I'll write up a simple example:
(I've removed header guards for simplicity and cleanliness)
The following is my globals.h and globals.cpp, respectively:
// Example of globals.h
#include <iostream>
#include <cstdio>
using namespace std;
class Globals {
public:
Globals ();
virtual ~Globals ();
void function1(char*);
void function2();
class Input *input;
class Error *error;
};
// Example of globals.cpp
#include "globals.h"
Globals::Globals()
{
input = new Input();
error = new Error();
}
void Globals::function1(char*nm)
{
cout << nm << endl;
}
Now, in my code for my Input class, say I want to use the function1(char*) method, would this be possible without passing an object to the Input class? What I mean by this is that I currently have my Input class being passed a *globals object, so then I could call the function like so: globals->function2();. But this can get very messy if I have a lot of functions within different classes. Additionally, is there a way I could use the Error pointer to object initialized in Globals? If Error had a function called error_func(), how could I be able to call it like so: error->error_func() from within my Input functions?
Thanks, and I apologize if I were too confusing in my question. I'll be happy to elaborate if needed.
Amit
EDIT 1: Added a simplified code to present what I want to do in a clearer way
// Example of globals.h
#include <iostream>
#include <cstdio>
#include "input.h"
#include "error.h"
using namespace std;
class Globals {
public:
Globals ();
virtual ~Globals ();
class Input *input;
class Error *error;
};
// Example of globals.cpp
#include "globals.h"
Globals::Globals()
{
input = new Input();
error = new Error();
}
// Example of input.h
#include "globals.h"
class Input {
public:
Input();
virtual ~Input();
}
// Example of input.cpp
#include "globals.h"
Input::Input()
{
error->print("Hello\n"); // <-- THIS is really what I want to accomplish (without being sent a globals object and say globals->error->print();
}
// Example of error.h
#include "globals.h"
class Error {
public:
Error() { }
virtual ~Error() { }
void print(char*);
}
// Example of error.cpp
#include "globals.h"
Error::print(char* nm)
{
cout << nm << endl;
}
If I'm understanding your question right, functions are automatically "inherited", at least for the purposes you need.
For example, your global class has two methods, function1(char*) and function2(). If you make a class:
class Descendent
: public Global
{ };
int main()
{
Global * global = new Global();
Global * desc = new Descendant();
char * str = "string";
// These two will run the same function:
global->function1(str);
desc->function1(str);
}
To prevent that (functions being called based on the current type), you must use virtual, like:
class Global
{
virtual void function1(char *);
};
class Descendant
{
virtual void function1(char *);
};
int main()
{
Global * global = new Global();
Global * desc = new Descendant();
char * str = "string";
// These two will NOT run the same function:
global->function1(str);
desc->function1(str);
}
Now, I'm not entirely sure, but the singleton idiom may be of use here, depending on just how global your Global is. In that case, you would have a global like:
class Global
{
static Global * GetSingleton()
{
if (!Global::m_Instance) Global::m_Instance = new Global();
return Global::m_Instance;
}
void function1(char *);
static Global * m_Instance;
};
class Descendant
{
void function1(char *)
{
Global * global = Global::GetGetSingleton();
// ...
}
};
There are a variety of ways to work with globals and functions being needed between classes. One of these may be it, depending on what exactly you're doing. If not, I'll try to edit and suggest one that does work.
I'm imagining you have a situation like this:
struct A {
void f();
};
struct B {
void g();
};
struct C : virtual A, virtual B {
C(A *ap, B *bp)
: A(ap), B(bp) // This doesn't actually work -- theoretical
{
}
void h()
{
f(); // calls A::f()
g(); // calls B::g();
}
};
Normally, when you create a C, you would be creating new As and Bs, but you would like to re-use existing ones instead, but still treat it like inheritance so that you don't have to explicitly specify which object to call.
Unfortunately, C++ doesn't support this. There are a couple of options:
You can make proxy classes that defer the function calls:
struct AProxy {
AProxy(A *ap) : a(*ap) { }
void f() { a.f(); }
A &a;
};
struct BProxy {
BProxy(B *bp) : b(*bp) { }
void g() { b.g(); }
B &b;
};
struct C : AProxy, BProxy {
C(A *ap,B *bp) : AProxy(ap), BProxy(bp) { }
void h()
{
f(); // calls AProxy::f() which calls a.f()
g(); // calls BProxy::g() which calls b.g()
}
};
This may help if you are using A's and B's in lots of different places.
If instead, you don't have many classes, but lots of calls to f() and g(), you might just do this:
struct C {
C(A *ap,B *bp) : a(*ap), b(*bp) { }
void f() { a.f(); }
void g() { b.g(); }
void h1()
{
f(); // well at least the call is clean here
g();
}
void h2()
{
f(); // and clean here
g();
}
A &a;
B &b;
};
If you don't have either of these cases, then just using the proper object each time like you were doing may be best.
Updated response:
Its sounds like what you want is actually the Factory pattern. I'm going to use logging as an example, where I assume that in one configuration you want to log and in another you might not want to:
// logger_interface.h
class LoggerInterface {
public:
virtual ~LoggerInterface() {}
virtual void Log(const string& message) = 0;
protected:
LoggerInterface() {}
};
The first step is to create a pure virtual interface representing the behavior that is configurable as in the example above. We will then create a factory function that can construct one based on configuration:
// logger_factory.h
LoggerInterface* CreateLogger(LoggerOptions options);
When implementing the factory, we keep the different implementations hidden:
// logger_factory.cc
class DoNotLogLogger : public LoggerInterface {
public:
DoNotLogLogger() {}
virtual ~DoNotLogLogger() {}
virtual void Log(const string& message) {}
};
class LogToStdErrLogger : public LoggerInterface {
public:
LogToStdErrLogger() {}
virtual ~LogToStdErrLogger() {}
virtual void Log(const string& message) {
std::cout << message << std::endl;
}
};
LoggerInterface* CreateLogger(LoggerOptions options) {
if (options.IsLoggingEnabled() && options.ShouldLogToStdErr()) {
return new LogToStdErrLogger;
}
return new DoNotLogLogger;
}
There is no reason why the object that you create dynamically in this way needs to be global; in fact, making it global is a really bad idea. Just create it where you need it, and pass it as a parameter to the functions that need it.
Original response:
Inheritance isn't the word you are looking for. Basically, what you are asking for is a static function:
class ClassName {
public:
static void methodName();
};
In the above, methodName can be invoked using ClassName::methodName() without requiring a specific instance of the class named ClassName. However, if you are to do this, it is more consistent with C++ style conventions to make it a freestanding function in a namespace like:
namespace name_of_namespace {
void functionName();
}
The above is invoked using name_of_namespace::functionName() as in the previous example, except with the benefit that it is easier to change or remove the prefix (e.g. via a using directive).
NOTE: from a design standpoint, you should only use a freestanding or static function if it does not rely on any state (other than the parameters passed to it) and there is no possibility of alternative implementations. As soon as there is state or alternative implementations, you really should pass around an object encapsulating this state, even if it is a pain to do, since passing around the object makes it easier to configure, makes it easier to mock-out in tests, and avoids threading issues.

How using dependency injection in a shared library (dll, so)?

Let's say in C++ I have an API in a shared library and I want to inject some dependence in it, at initialization. How can I do that ?
For example, in the calling code :
#include "CTest1.h"
#include "CTest2.h"
...
#include "CTest15.h"
class CTest{
int att1;
int att2;
}
and in the shared library :
#include "CTest.h"
class export CSharedObject{
void create(CTest* test){ mtest = test; }
void doSomething(){ int sum = test->att1+test->att2; }
CTest* mtest;
}
How can I inject CTest in CSharedObject ? Just with the include ? But CSharedObject will need all the files CTest needs
I need to avoid all these includes
First, your class needs an interface :
class CTestIface
{
virtual ~CTestIface(){}
virtual void foo() = 0;
};
Then in the library add an implementation :
class CTest1 : public CTestIface
{
void foo(){ /*...*/ }
};
and c functions to create/delete objects of type CTest11:
extern C{
void* Create() { return new CTest1; };
void Delete( void* o ) { delete( (CTest1*) o ); }
}
As you can see, you just need a header defining the interface, and functions to create/delete objects.
by the way, in tests, you should use mock classes (inheriting from the interface)

Several C++ classes need to use the same static method with a different implementation

I need several C++ classes to have a static method "register", however the implementation of register varies between those classes.
It should be static because my idea is to "register" all those classes with Lua (only once of course).
Obviously I can't declare an interface with a static pure virtual function. What do you guys suggest me to do ? Simplicity is welcome, but I think some kind of template could work.
Example of what I would like to achieve
class registerInterface
{
public:
static virtual void register() = 0; //obviously illegal
};
class someClass: public registerInterface
{
static virtual void register()
{
//I register myself with Lua
}
}
class someOtherClass: public registerInterface
{
static virtual void register()
{
//I register myself with Lua in a different way
}
}
int main()
{
someClass::register();
someOtherClass::register();
return 0;
}
Based on how you've described the problem, it's unclear to me why you even need the 'virtual static method' on the classes. This should be perfectly legal.
class SomeClass {
static void register(void) {
...
}
}
class SomeOtherClass {
static void register(void) {
...
}
}
int main(int argc, char* argv[]) {
SomeClass::register();
SomeOtherClass::register();
return 0;
}
Drop the RegisterInterface, I don't think you need it.
If it helps, you could take Hitesh's answer, and add:
struct luaRegisterManager {
template <typename T>
void registrate() {
T::registrate();
// do something else to record the fact that we've registered -
// perhaps "registrate" should be returning some object to help with that
}
};
Then:
int main() {
luaRegisterManager lrm;
lrm.registrate<someClass>();
lrm.registrate<someOtherClass>();
}
More generally, if you want to introduce any dynamic polymorphism in C++, then you need an object, not just a class. So again, perhaps the various register functions should be returning objects, with some common interface base class registeredClass, or classRegistrationInfo, or something along those lines.
Could provide an example of what you feel it is that you need dynamic polymorphism for? Hitesh's code precisely matches your one example, as far as I can see, so that example must not cover all of your anticipated use cases. If you write the code that would be using it, perhaps it will become clear to you how to implement it, or perhaps someone can advise.
Something else that might help:
#include <iostream>
#include <string>
#include <vector>
struct Registered {
virtual std::string name() = 0;
virtual ~Registered() {}
Registered() {
all.push_back(this);
}
static std::vector<Registered*> all;
};
std::vector<Registered*> Registered::all;
typedef std::vector<Registered*>::iterator Iter;
template <typename T>
struct RegisteredT : Registered {
std::string n;
RegisteredT(const std::string &name) : n(name) { T::registrate(); }
std::string name() { return n; }
// other functions here could be implemented in terms of calls to static
// functions of T.
};
struct someClass {
static Registered *r;
static void registrate() { std::cout << "registering someClass\n"; }
};
Registered *someClass::r = new RegisteredT<someClass>("someClass");
struct someOtherClass {
static Registered *r;
static void registrate() { std::cout << "registering someOtherClass\n"; }
};
Registered *someOtherClass::r = new RegisteredT<someOtherClass>("someOtherClass");
int main() {
for (Iter it = Registered::all.begin(); it < Registered::all.end(); ++it) {
std::cout << (*it)->name() << "\n";
}
}
There are all sorts of problems with this code if you try to split it across multiple compilation units. Furthermore, this kind of thing leads to spurious reports from memory leak detectors unless you also write some code to tear everything down at the end, or use a vector of shared_ptr, Boost pointer vector, etc. But you see the general idea that a class can "register itself", and that you need an object to make virtual calls.
In C++ you usually try to avoid static initialisation, though, in favour of some sort of setup / dependency injection at the start of your program. So normally you would just list all the classes you care about (calling a function on each one) rather than try to do this automatically.
Your intentions are noble, but your solution is inkling towards "overengineering" (unless I am missing an obvious solution).
Here is one possibility: You can use the Virtual Friend function idiom For example,
class RegisterInterface{
friend void register(RegisterInterface* x){x->do_real_register();}
protected:
virtual void do_real_register();
}
class Foo : public RegisterInterface{
protected:
virtual void do_real_register(){}
};
class Bar : public RegisterInterface{
protected:
virtual void do_real_register(){}
};
int main(int argc, char* argv[]) {
BOOST_FOREACH(RegisterInterface* ri, registered_interfaces)
{
register(ri);
}
return 0;
}
I know you've already accepted an answer, but I figured I would write this up anyway. You can have self-registering classes if you use some static initialization and the CRTP:
#include <vector>
#include <iostream>
using namespace std;
class RegisterableRoot // Holds the list of functions to call, doesn't actually need
// need to be a class, could just be a collection of globals
{
public:
typedef void (*registration_func)();
protected:
static std::vector<registration_func> s_registery;
public:
static void do_registration()
{
for(int i = 0; i < s_registery.size(); ++i)
s_registery[i]();
}
static bool add_func(registration_func func) // returns something so we can use it in
// in an initializer
{
s_registery.push_back(func);
return true;
}
};
template<typename RegisterableType> // Doesn't really need to inherit from
class Registerable : public RegisterableRoot // RegisterableRoot
{
protected:
static const bool s_effect;
};
class A : public Registerable<A> // Honestly, neither does A need to inherit from
// Registerable<T>
{
public:
static void Register()
{
cout << "A" << endl;
}
};
class B : public Registerable<B>
{
public:
static void Register()
{
cout << "B" << endl;
}
};
int main()
{
RegisterableRoot::do_registration();
return 0;
}
std::vector<RegisterableRoot::registration_func> RegisterableRoot::s_registery;
template <typename RegisterableType> // This is the "cute" part, we initialize the
// static s_effect so we build the list "magically"
const bool Registerable<RegisterableType>::s_effect = add_func(&RegisterableType::Register);
template class Registerable<A>; // Explicitly instantiate the template
// causes the equivalent of
// s_registery.push_back(&A::Register) to
// be executed
template class Registerable<B>;
This outputs
A
B
although I wouldn't rely on this order if I were you. Note that the template class Registerable<X> need not be in the same translation unit as the call to do_registration, you can put it with the rest of your definition of Foo. If you inherit from Registerable<> and you don't write a static void Register() function for your class you'll get a (admittedly probably cryptic) compiler error much like you might expect if there really was such a thing as "static virtuals". The "magic" merely adds the class specific function to the list to be called, this avoids several of the pitfalls of doing the actual registration in a static initializer. You still have to call do_registration for anything to happen.
How about this way? Define an interface class:
// IFoobar.h
class IFoobar{
public:
virtual void Register(void) = 0;
}
Then define the class that handles the register..
// RegisterFoobar.h
class RegisterFoobar{
public:
// Constructors etc...
IFoobar* fooBar;
static void RegisterFoobar(IFoobar& fubar){
foobar = &fubar;
}
private:
void Raise(void){ foobar->Register(); }
}
Now, then define another class like this
// MyFuBar.h
class MyFuBar : IFoobar{
public:
// Constructors etc...
void Register(void);
private:
RegisterFoobar* _regFoobar;
}
Call the code like this:
//MyFuBar.cpp
MyFuBar::MyFuBar(){
_regFoobar = new Foobar();
_regFoobar->RegisterFoobar(this);
}
void MyFuBar::Register(void){
// Raised here...
}
Maybe I have misunderstood your requirements...