Solved

VC++ initalization

Posted on 2000-05-03
8
246 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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…

760 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

18 Experts available now in Live!

Get 1:1 Help Now