Solved

VC++ initalization

Posted on 2000-05-03
8
257 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
[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
  • 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
Industry Leaders: 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!

 
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
 

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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
Which IDE to use to begin C++ training? 5 76
Unresolved External Symbols 3 154
How to convert Structure to vector of byte and vice versa 5 38
Gaming Software 1 38
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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. …
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

734 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