how to use dlopen with C++ class object

I have shared library that has class and class methods

I want to load this shared library at run time using dlopen ,

Can some one can tell me basic example of the same
Last edited on
In the above code is there any problem and is there any

It is your code. You know whether or not your code has problems.

Please do not expect us to find the problems for you. Even if we are problem solvers, we have no clue.
Yes there are problems - you forgot to delete b and have a memory leak.
Also if(b) is useless since new will throw bad_alloc if there is not enough memory.
Class A doesn't have a method setX.
Sorry my question was

In the above code is there any case where getA() can return a NULL pointer or invalid pointer . If yes what is valid check for it ?
There is no way that getA() could possibly return a null pointer. The object from B must of course be valid but checking the result of getA() doesn't make sense nonetheless.
In the above code is there any case where getA() can return a NULL pointer or invalid pointer.

The simple answer is no, as long as b is valid.

The longer answer is if b is not valid, then any reference returned by getA() is also invalid and will result in undefined behavior.
Consider:
1
2
3
4
5
  B * b = new B; 
  //  Do something with b 
  delete b;
  b = nullptr; 
  //  b is no longer valid 


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Last edited on
Topic archived. No new replies allowed.