CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2004
    Posts
    116

    Singleton? Static member pointer? Design question...

    I have an object that lives throughout the life of an app. There are also DLLs that access the object to read _and_ write it. It is a pointer and is a data member of the Application (CWinApp). Obvisously this is not ideal and I inherited it, so Id like to address the issue, as currently there are AfxGetApp() calls everywhere and its ugly.

    Suggestions? Where should the object live? and how best to access it?
    Thx.

  2. #2
    Join Date
    Aug 2011
    Posts
    10

    Re: Singleton? Static member pointer? Design question...

    I am stupid and don't know anything about you problem, I hope to know but I can never, calling a function multiple times when needed is better than calling once to be used by all

  3. #3
    Join Date
    Jan 2004
    Posts
    116

    Re: Singleton? Static member pointer? Design question...

    I am stupid and don't know anything about you problem
    awesome

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Singleton? Static member pointer? Design question...

    Well, it sounds like singletone is a way to do that. Besides, 'read' and 'write' functions exported from main app will allow dlls exchange data with the object in safe manner.
    Best regards,
    Igor

  5. #5
    Join Date
    Aug 2008
    Posts
    112

    Re: Singleton? Static member pointer? Design question...

    Quote Originally Posted by gareth19 View Post
    awesome
    You at least need to learn to say thanks for what people post to help you. Your complement on such negative thinking about self only shows how low your common sense actually is.
    hi,,,

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Singleton? Static member pointer? Design question...

    Quote Originally Posted by gareth19 View Post
    I have an object that lives throughout the life of an app. There are also DLLs that access the object to read _and_ write it. It is a pointer and is a data member of the Application (CWinApp). Obvisously this is not ideal and I inherited it, so Id like to address the issue, as currently there are AfxGetApp() calls everywhere and its ugly.

    Suggestions? Where should the object live? and how best to access it?
    Thx.
    In MFC, there is nothing wrong with following this approach.

    The CWinApp object is essentially a singleton and the best practice way to access it is AfxGetApp().

    So AfxGetApp()->GetMyClass()->DoSomeWork() is perfectly fine.

    If you made this a singleton, it would be something like MyClass::GetInstance()->DoSomeWork() which isn't significantly better than what you already have.

    If you keep the AfxGetApp() approach, you have something that is well understood by anyone that programs with MFC.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured