MFC Tips - How to Declare Global Variable & Function

zhaozj2021-02-16  101

How to declare global variables or functions in MFC

We can declare static member variables or member functions in CWINAPP derived classes, so you can access them from all classes:

E.g:

// myapp.h

Class CMYAPP: Public CWINAPP

{

PUBLIC:

CMYAPP ();

Static int g_nmyvariable; // Declare a global variable

Static void g_myfunction (); // Declare a global function

...

ETC

...

}

// myapp.cpp

INT CMYAPP :: g_nmyvariable = null; // global variable definition and initialization CMYAPP :: g_myfunction () // global function definition

{

}

Now, this variable and function can be accessed from other classes:

CMYAPP :: G_nmyvariable and cmyapp :: g_myfunction

转载请注明原文地址:https://www.9cbs.com/read-26268.html

New Post(0)