Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VC++ initalization

Posted on 2000-05-03
8
Medium Priority
?
272 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
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!

 
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 400 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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

805 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