0

I am trying to create a static member function that returns a pointer to one instance of the class. Is this possible in C++?

    class DynamicMemoryLog
{
    // Singleton Class:

    public:

        static DynamicMemoryLog* CreateLog();
        void   AddIObject( IUnknown* obj );
        void   ReleaseDynamicMemory();

    private:
        // static DynamicMemoryLog* instance;
        static bool isAlive;  // used to determine is an instance of DynamicMemoryLog already exists

        DynamicMemoryLog();
        ~DynamicMemoryLog();

        std::vector <IUnknown*> iObjectList;
};

This function below should create a new instance of the class & return a pointer to that object, but the compiler will not allow me to define a static function of the class if it returns a pointer(I think thats why it wont compile?):

static DynamicMemoryLog* DynamicMemoryLog :: CreateLog()
{
    // Post:

    if ( !isAlive )   // ( instance == NULL; )
    {
       DynamicMemoryLog* instance  = new DynamicMemoryLog();
       return instance;
    }

    return NULL;
}
2
  • DynamicMemoryLog.cpp(17): error C2724: 'DynamicMemoryLog::CreateLog' : 'static' should not be used on member functions defined at file scop Commented Jan 18, 2011 at 10:36
  • just a reminder, if you have more than one singleton class linked or depend each other, logging classes should be removed or released as last during program exit. Therefore, please keep in mind "object life time" management. It is usually skipped by most developers. Commented Jan 18, 2011 at 12:26

3 Answers 3

2

The particular error you're getting is that when implementing a static member function, you don't repeat the static keyword. Fixing this should resolve the error.

Independently, there's something a bit odd with your code. You claim that this object is a singleton, but each call to CreateLog will create a new instance of the class. Do you really want this behavior, or do you want there to be many copies? I'd suggest looking into this before proceeding.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, so I dont put static at the begininng when implementing the static function. Thx :)
1

Here's the simplest solution, but not thread-safe. For analysis in detail, have a look at this article.

class DynamicMemoryLog 
{
public:
   static DynamicMemoryLog* GetInstance();
private:
   DynamicMemoryLog();
   static DynamicMemoryLog* m_pInstance;
}

DynamicMemoryLog* DynamicMemoryLog::GetInstance()
{
   if(!m_pInstance)    
   {
      m_pInstance = new DynamicMemoryLog();       
   }     

   return m_pInstance;
}

3 Comments

If GetInstance() is not static, I wont be able to do this will I... DynamicMemoryLog *dm = DynamicMemoryLog :: GetInstance(); ?
@Sascha Static member methods can access only static class' attributes
@Sascha You would usually access other members through DynamicMemoryLog::GetInstance(), e.g. DynamicMemoryLog::GetInstance()->AddIObject(...)
0

I usually do something like this:

class Singleton
{
    public:

        static Singleton* get()
        {
            static Singleton instance;
            return &instance;
        }
};

This way you won't have any nasty memory management issues.

2 Comments

Will that variable, instance, will be persistant throughout & fall out of scope?
@Sascha: what do you mean with persistent and fall out of scope? The variable instance will be constructed when get() is called for the first time and destructed on program exit (or on dynamic library unload when the class lives in a library).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.