Re: How to Define a global const class variable in MFC?



Just an opinion...

I would not make this a global variable. Instead I would probably add it to the main app class or perhaps, a class that contains other variables like this that is, itself, hosted in the main app class. That way you could access it with code like:

theApp.m_InfoArray.Add()

Which lets you know where it is hosted and also you could easily clean it up when exiting the application instance. This is also very easy to debug.

I try very hard not to use global variables these days and I've found it makes debugging and cleaning up much easier.

Tom

"Electronic75" <Electronic75@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:420537CC-30E0-4A6A-B24B-A54AC08B8AC2@xxxxxxxxxxxxxxxx
Hello :), I'm trying to define a global const class varibles in a MFC
application. This class holds an array of variables that I like to assign at
the beginning and I will use this variable and its stored const values in my
program in different classes. Now what I'm doing is to add a blank cpp file
to my project and then I define this class variable there.


#include "MyInfoArrayClass.h"

const CMyInfoArrayClass g_Info;
g_Info.Add(data1);
g_Info.Add(data2);
...


but compiler dose not see the definition and intellisense shows the type of
g_Info as int and returns SYNTAX error and redefinition of basic type. Even
when I place class definition in a header file and include header file in the
source file compiler returns same errors.
I have done this with a char type or int type, I mean I have defined a
global char or int variable, and I had no problem but when I want to define a
class type variable I encounter trouble.
Where I made a mistake?
thanks :)


.



Relevant Pages

  • Re: Moving from 8051 to AVR
    ... Getting rid of global variables is not "good programming practice". ... int foo; ...
    (comp.arch.embedded)
  • Re: printing the permutation, help
    ... People might not be able to see the post you are replying to. ... Don't use global variables without a very good reason. ... Implicit int was removed in the C99 standard and many considered it bad style even before then, and you don't return a value anyway! ... there are plenty of solutions without bothering with recursion. ...
    (comp.lang.c)
  • Re: Resources about program design in C
    ... an interest in the reasoning behind global variables in C programs. ... A function should isolate its implementation details from the ... imagine a module that implements a stack ... static int stack_ptr; ...
    (comp.lang.c)
  • Re: How do you declare a Global Variable in VC++6
    ... Tom Surface described how to declare/define globals variables in his initial reply. ... However, global variables are almost always a bad idea, as others have noted. ... If they belong to the app class, there should be no reason the doc or view need to see ... What does your StringTime class do that requires global variables? ...
    (microsoft.public.vc.mfc)
  • Re: Regarding use of modules
    ... other functions that when called will update some of the global variables defined in mainfile.c. ... When there is information which more than one module needs, such as declarations for functions, structures, and global variables, a recommended way to achieve this is with a header file that is included in multiple code files as needed. ... The header file associated with the code file contains all the declarations and information needed to use lcd.c. ...
    (comp.lang.c)