名为MyPrg的MFC项目,在MyPrg.cpp文件中有 CMyPrgApp theApp;这么一个语句 它不在类的{}内,但在MyPrg.cpp文件中,它是啥

eisldkw 2017-07-12 03:42:30
名为MyPrg的MFC项目,在MyPrg.cpp文件中有 CMyPrgApp theApp;这么一个语句 它不在类的{}内,但在MyPrg.cpp文件中,它是啥,是全局变量,还是么有变量 ,但它前面也没有public 或 private这些限定词?它到底是什么?

MyPrg.cpp文件 具体内容如下

// MyPrg.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "MyPrg.h"
#include "MyPrgDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyPrgApp

BEGIN_MESSAGE_MAP(CMyPrgApp, CWinApp)
//{{AFX_MSG_MAP(CMyPrgApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyPrgApp construction

CMyPrgApp::CMyPrgApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CMyPrgApp object

CMyPrgApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CMyPrgApp initialization

BOOL CMyPrgApp::InitInstance()
{
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.

#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif

CMyPrgDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}

// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
...全文
203 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
eisldkw 2017-07-13
  • 打赏
  • 举报
回复
引用 5 楼 schlafenhamster 的回复:
extern CMyPrgApp theApp;是声明(外部)
非常感谢,为什么不能在其他CPP文件中以CMyPrgApp theApp;这种形式直接定义 theApp这个全局变量呢(当然MyPrg.cpp去掉CMyPrgApp theApp;这句话)?
schlafenhamster 2017-07-13
  • 打赏
  • 举报
回复
extern CMyPrgApp theApp;是声明(外部)
eisldkw 2017-07-13
  • 打赏
  • 举报
回复
引用 3 楼 schlafenhamster 的回复:
是的 extern CMyPrgApp theApp;
非常感谢,但extern CMyPrgApp theApp;是引用吧,不是用来定义这个 theApp 变量的吧?
schlafenhamster 2017-07-13
  • 打赏
  • 举报
回复
为什么不能在其他CPP文件中以CMyPrgApp theApp 因为这样定义是一个 新的 theApp 与 原来 App 中的 无 任何关系。 theApp 这是 MFC 的 语法 糖。 其实就是 AfxGetApp CWinApp* AfxGetApp( ); Return Value A pointer to the single CWinApp object for the application. Remarks The pointer returned by this function can be used to access application information such as the main message-dispatch code or the topmost window.
schlafenhamster 2017-07-12
  • 打赏
  • 举报
回复
是的 extern CMyPrgApp theApp;
eisldkw 2017-07-12
  • 打赏
  • 举报
回复
引用 1 楼 schlafenhamster 的回复:
CMyPrgApp theApp; 这句 定义了 一个 全局变量。 这样 在别的类中 可以 这样使用 extern CMyPrgApp theApp; theApp.WriteProfileString(szSection_VSELECT,tmp,NULL);// app 函数 。。 theApp.Read802NcVariabl(m_VarsIdx,bt_pars,&retBuf);// 自己写的 在 App 中 的 函数
非常感谢,请问,按您的意思,是不是这个CMyPrgApp theApp; 也可以直接剪切并放置到这个工程项目的其他的CPP文件中,也可以正常使用?
schlafenhamster 2017-07-12
  • 打赏
  • 举报
回复
CMyPrgApp theApp; 这句 定义了 一个 全局变量。 这样 在别的类中 可以 这样使用 extern CMyPrgApp theApp; theApp.WriteProfileString(szSection_VSELECT,tmp,NULL);// app 函数 。。 theApp.Read802NcVariabl(m_VarsIdx,bt_pars,&retBuf);// 自己写的 在 App 中 的 函数

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧