Solved

VC++ initalization

Posted on 2000-05-03
8
248 Views
Last Modified: 2010-04-02
What is the best way to initialize a program? I am using the InitInstance to initialize the variable in a generic class however the values don’t stay outside CApp class. What am I doing wrong?

Here is an example of what I am trying to do.
I have made a new class in VC++ 6 like the following:

class data  
{
public:
      int age;
      data();
      virtual ~data();

};


and I like to initialize age to 20 when the program starts, I did the following:

BOOL CMineApp::InitInstance()
{
      data temp;
      temp.age = 20;


some codes

}

how ever age dose not hold this number outside the CmineApp class

I know I have some type of variable scoping;

I hope you can help me, let me know

Thanks
Matt

0
Comment
Question by:mrfard
  • 4
  • 3
8 Comments
 
LVL 2

Expert Comment

by:abdij
ID: 2775181
Hi,

 The questions not quite clear. Could u send ur code to abdij_b@hotmail.com with relevent comment in the code. I donot get what u mean by outside the doc. How have u declared the variable? What is the scope where do u want to use it? Lot of quetions unclear.

Bye
abdij
0
 

Author Comment

by:mrfard
ID: 2775262
Edited text of question.
0
 

Author Comment

by:mrfard
ID: 2775585
Adjusted points from 50 to 100
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 2775636
Your could declare a data member variable for your CWinApp derived class CMineApp.

class CMineApp : public CWinApp
{
public:
  data m_AppData;

....

You could access data outside of your CMineApp using the following macro:

#define GetAppData()   \
  (((CMineApp*) AfxGetApp())->m_AppData)

Like so:

GetAppData().age = 20;

Good Luck,
Steve




0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:mrfard
ID: 2777293
Steve;

I really appreciate your response, however I my not have been clear in my question. I like to manipulate (write or read) the variable in the data class from any class in the program (generic or MFC). Any idea what would be the best way. It seems my data get lost from one class to the other.

Thanks
Matt
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 100 total points
ID: 2777393
My method is globally accessible from any class. Your original example declared an instance in a function. This loses scope once you exit the function. You could make it static within the function to retain any values you may have set, but then that instance would only be available in the function. You could create a container class with a static instance of data. But, this is just a variation of my example where the CWinApp derived class is the container.
0
 

Author Comment

by:mrfard
ID: 2777615
Thanks Steve, I got it to work the way you said, it is weird. Does the work for multi threading?

0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 2777802
It could be if you put the proper code in your data class accessors. You'd want to have a PutAge() and GetAge() member function that messed with the actual data from within critical section protectors. I'm no expert on that, but there is code in the MFC code that could serve as an example. See AFXTLS.CPP's and search for "CriticalSection".

Good Luck,
Steve
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now