Solved

static variable in mfc

Posted on 2001-07-26
3
318 Views
Last Modified: 2013-11-20
I would like to use a static variable as member data of an class in an mfc dialog based application.Here where should I do forward declaration?

eg:

class dlgMyclass
{
  static int a;
  public:
  void say()
  {
     AfxMessageBox("hi");
   }
};

where to do forward declaration i.e.  int dlgMyclass::a;

Please suggest me the solution for this.
0
Comment
Question by:joshig
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 6322104
>>where to do forward declaration i.e.  int dlgMyclass::a;

You don't need a foward declaration, all you have to do is instantiating storage space for the variable:

class dlgMyclass
{
 static int a;
 public:
 void say()
 {
    AfxMessageBox("hi");
  }
};

int dlgMyclass::a = 0;
0
 
LVL 30

Expert Comment

by:Axter
ID: 6323901
FYI,
Adding to jkr comment, you usally want to put the implementation in the *.cpp file.
So
//Put in header (*.h) file
class dlgMyclass
{
static int a;
public:
void say()
{
   AfxMessageBox("hi");
 }
};
/////////////////////////////////

//Put in source (*.cpp) file
int dlgMyclass::a = 0;

0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9490562
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by : jkr

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question