VC ++中的全局变量声明 [英] Global Variable declaration in VC++

查看:122
本文介绍了VC ++中的全局变量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用MFC做基于对话框的应用程序.我的问题看起来很简单,但我收到的页面有很多错误.

我有一个从CDialog派生的类.

class CSampleDialogLogicDlg : public CDialog
{
};


并且具有两个从CSampleDialogLogicDlg派生的子类.

class CMlkOne : public CSampleDialogLogicDlg 
{
};
class CMlkTwo : public CSampleDialogLogicDlg
{
};



而且我在普通头文件中有一组变量.我需要从我的两个子类中访问这些变量.因此,我认为将该头文件包含在我的基类CSampleDialogLogicDlg头文件中就足够了.

但是我正在为所有变量获取变量已经定义的错误.

我应该怎么做才能在整个项目中将标头中的所有这些变量都设置为全局变量.我在这里缺少任何内容吗?

请帮助我.

湿婆

[edit]代码块,忽略HTML ..."选项已禁用-OriginalGriff [/edit]

解决方案

在标头中定义任何对象都是一个坏主意文件.使用类型或函数的声明就可以了,它们可以重复.对于该对象,您需要在头文件中声明extern,并在某些C ++中分别定义(有时人们仅出于此目的而使用C文件).这样,您可以有多个声明,但可以有唯一的定义.如果您无法以这种方式组织它,则链接器将抱怨:它需要全局对象的唯一符号.顺便说一句,知道声明定义之间的区别.

我将通过使用类的静态成员来避免这种情况.避免使用任何静态(全局")声明甚至更好.始终可以重新设计对象之间的协作.

—SA


您应该:



在头文件中
  • 声明这样的变量,例如extern.在一个源文件中,
  • 定义这样的变量.







一个简单的例子:

(common.h)

 //  .. 
外部  int  g_count;
//  ..  



(main.cpp)

 //  ..(您可以选择包括common.h)
 int  g_count =  0 ;
//  ..  



(other.cpp)

 #include   "  common.h"
 //  ..在此处使用"g_count"  






如果两个派生类都需要访问多个变量,则将这些变量包括在基类本身中,则将有一个用于每个类实例,如果需要访问相同的实例,请考虑使其成为静态成员,这比使其成为全局实例更好.

Hi Everyone there,

I am doing a dialog based application using MFC. My problem looks very simple, but i am getting pages of errors.

I have a class derived from CDialog.

class CSampleDialogLogicDlg : public CDialog
{
};


And have two sub classes derived from CSampleDialogLogicDlg.

class CMlkOne : public CSampleDialogLogicDlg 
{
};
class CMlkTwo : public CSampleDialogLogicDlg
{
};



And I have set of variables in a normal header file. I need to access those variables from both of my subclasses. So i thought, it is enough to include that header file in my base class CSampleDialogLogicDlg header file.

But I am getting Variable already defined error for all the variables.

What should i do to get all those variables in header as global through out project.Am i missing anything here?

Pls help me in this.

Shiva.

[edit]Code blocks, "Ignore HTML..." option disabled - OriginalGriff[/edit]

解决方案

It''s a bad idea to define any objects in header file. With declarations of types or functions it''s fine, they can repeat. With the object, you need to declare as extern it in header file and define it separately in some C++ (sometimes people use C files just for this purpose). In this way, you can have multiple declaration but unique definition. If you fail to organize it in this manner, the linker will complain: it needs unique symbol for a global object. By the way, know the difference between declaration and definition.

I would avoid it by using static members of classes. Avoiding any static ("global") declarations is even better. It''s always possible to re-design collaboration between your objects.

—SA


You should:



  • Declare such variables as extern in the header file.
  • Define such variables in one source file.







A simple example:

(common.h)

//..
extern int g_count;
//..



(main.cpp)

//.. (you may optionally include common.h)
int g_count=0;
//..



(other.cpp)

#include "common.h"
//.. use 'g_count' here






if you have multiple variables that need to be accessed by both derived classes, include the variables in the base class itself, you''ll have one variable for each class instance, if you need access to the same one, consider making it a static member, this should be a better option than making it global.


这篇关于VC ++中的全局变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆