static pointer is always null used in the plugin

K

ken

hello,
class App {

};

class Scene {
static Scene *s_pScene;
static Scene * instance() {
if (!s_pScene)
s_pScene = new Scene;
return s_pScene;
};
Scene * Scene::s_pScene = 0;

//---
the App *app is passed to a plugin loaded by LoadLibrary...
but the Scene::instace() return null always, why?
 
S

Saeed Amrollahi

hello,
class App {

};

class Scene {
static Scene *s_pScene;
static Scene * instance() {
  if (!s_pScene)
    s_pScene = new Scene;
  return s_pScene;};

Scene * Scene::s_pScene = 0;

//---
the App *app is passed to a plugin loaded by LoadLibrary...
but the Scene::instace() return null always, why?

Hi

I think, because you didn't call the Instance member function in app.
Here is a typical use of Singleton pattern and calling Instance
inside App ctor:

#include <iostream>
struct Singleton {
static Singleton* pSingleton;
static Singleton* Instance()
{
if (!pSingleton)
pSingleton = new Singleton();
return pSingleton;
}
};

struct App {
App() { Singleton::Instance(); }
};

Singleton* Singleton::pSingleton = 0;
int main()
{
using namespace std;
App app;
if (!Singleton::pSingleton)
cout << "Imposibble ..." << '\n';
return 0;
}

Regards,
-- Saeed Amrollahi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top