Solved

VC++ initalization

Posted on 2000-05-03
8
254 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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 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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

828 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