Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Using an Object declared in main() inside a header

Using an Object declared in main() inside a header

Scheduled Pinned Locked Moved C / C++ / MFC
debugging
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rajesh_Parameswaran
    wrote on last edited by
    #1

    Hi all, I've declared an object of type CTestLogger in my main function. Now I want that object to be accessed in one of the global function, which is defined in a header file. Please find the code snippet and Suggest me a solution if i'm wrong.!! NB: I cant modify the parameters of the global function: Display_Printf() ============================================================================= #include "Debug_Print.h" void main() { CTestLogger bac; Initialize_Logger(&bac); Display_Printf(); DeInitialize_Logger(); bac.SetMessage("Message From Main"); Initialize_Logger(&bac); Display_Printf(); Display_Printf("Custom Message From Main"); DeInitialize_Logger(); } ============================================================================= #include #include class CTestLogger { char pTestMessage[1024]; public: CTestLogger() { strcpy(pTestMessage, "Default Message"); cout << "Constructor" << endl; } void SetMessage(char *pMessage) { strcpy(pTestMessage, pMessage); } ~CTestLogger() { cout << "Destructor" << endl; } void DisplayMessage() { cout << "Display Message :: " << pTestMessage << endl; } }; ============================================================================= #include #include "Custom_Header.h" CTestLogger *g_pTestLogger = NULL; // MUST BE CALLED before invking both the Display_Printf() void Initialize_Logger(CTestLogger *pTestLogger) { g_pTestLogger = pTestLogger; } void Display_Printf() { if (g_pTestLogger) g_pTestLogger->DisplayMessage(); else cout << "Invalid CTestLogger Pointer" << endl; } void Display_Printf(char *pMessage) { if (g_pTestLogger) { g_pTestLogger->SetMessage(pMessage); g_pTestLogger->DisplayMessage(); } else cout << "Invalid CTestLogger Pointer" << endl; } // MUST DEINITIALIZE IF Initialze_Logger is called.!!!!!! void DeInitialize_Logger() { g_pTestLogger = NULL; } ============================================================================ thanks in advance Rajesh

    D 1 Reply Last reply
    0
    • R Rajesh_Parameswaran

      Hi all, I've declared an object of type CTestLogger in my main function. Now I want that object to be accessed in one of the global function, which is defined in a header file. Please find the code snippet and Suggest me a solution if i'm wrong.!! NB: I cant modify the parameters of the global function: Display_Printf() ============================================================================= #include "Debug_Print.h" void main() { CTestLogger bac; Initialize_Logger(&bac); Display_Printf(); DeInitialize_Logger(); bac.SetMessage("Message From Main"); Initialize_Logger(&bac); Display_Printf(); Display_Printf("Custom Message From Main"); DeInitialize_Logger(); } ============================================================================= #include #include class CTestLogger { char pTestMessage[1024]; public: CTestLogger() { strcpy(pTestMessage, "Default Message"); cout << "Constructor" << endl; } void SetMessage(char *pMessage) { strcpy(pTestMessage, pMessage); } ~CTestLogger() { cout << "Destructor" << endl; } void DisplayMessage() { cout << "Display Message :: " << pTestMessage << endl; } }; ============================================================================= #include #include "Custom_Header.h" CTestLogger *g_pTestLogger = NULL; // MUST BE CALLED before invking both the Display_Printf() void Initialize_Logger(CTestLogger *pTestLogger) { g_pTestLogger = pTestLogger; } void Display_Printf() { if (g_pTestLogger) g_pTestLogger->DisplayMessage(); else cout << "Invalid CTestLogger Pointer" << endl; } void Display_Printf(char *pMessage) { if (g_pTestLogger) { g_pTestLogger->SetMessage(pMessage); g_pTestLogger->DisplayMessage(); } else cout << "Invalid CTestLogger Pointer" << endl; } // MUST DEINITIALIZE IF Initialze_Logger is called.!!!!!! void DeInitialize_Logger() { g_pTestLogger = NULL; } ============================================================================ thanks in advance Rajesh

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Rajesh_Parameswaran wrote:

      I've declared an object of type CTestLogger in my main function. Now I want that object to be accessed in one of the global function...

      No can do. bac is only accessible by main(). Why did you supply such a large code (irrelavant) snippet? :confused:


      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

      "Judge not by the eye but by the heart." - Native American Proverb

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups