I'm using framework which has a class Register, into which I'm able to register instances of A.
Register r;
A * a1 = new A();
r->register(a1);
A * a2 = new A();
r->register(a2);
Register will take ownership and deletes all registered A
s when going out of scope.
I want to modify A
s behaviour in shared library (.so in my case), so, I'm gonna do something like this (in the library):
class B : public A {
...
}
B * get_customized_a() {
return new B();
}
And then in main program
Register r;
A * a1 = get_customized_a();
r->register(a1);
but now a1
will be deleted in main program, not in the library! Which from I understud is a big no-no.
So how to solve this?
I've come up with two solution:
1) Use A and customize it by stand-alone function
in plugin:
void customize_a(A * a) { ... }
in main program:
Register r;
A * a1 = new A();
customize_a(a1);
r->register(a1);
I must say I don't like it that much :/
2) Overload delete operator for class B
in plugin
class B : public A {
...
static void operator delete(void * ptr) {
::operator delete(ptr);
}
}
in main program:
Register r;
A * a1 = get_customized_a();
r->register(a1);
However, I never overloaded operator delete
before so I'm not sure if this will even work (as intended).
3) Is there any approach I missed? Is there a better solution?
Thank you all.