Solved

How do I create a global instance of an object in visual C++

Posted on 2004-08-07
6
127 Views
Last Modified: 2010-04-24
Ok what I am doing is creating an objct and filling it with data from a file.  But when that function finishes the object is out of scope.  In c++ I would have just made an instace of that object at the top of my main{} in my main.cpp and pointed to it from my function also in my main.cpp.  In visual c++  there is no main.cpp (or at least I cant figure out how to access it if I do make one). So where would I declare an instance of the class  that I do not want to go out of scope? Or is there a specific way I have to do this?  Thank you for any help you can give!
0
Comment
Question by:Rackboy
  • 3
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11744779
> In visual c++  there is no main.cpp (or at least I cant figure out how to access it if I make one).
In VC++ you can consider your main file where is located main(), WinMain() or your CWinApp derived object implementation.
But this doesn't matters. You can declare your global object where you consider is related (at the top of a cpp file).
If you want to use it from another cpp file just declare it as external at the top of this file.

extern CMyObject myInstance;
0
 

Author Comment

by:Rackboy
ID: 11744803
Do I put it in the .h or the .cpp?
when I try and access it from the .h file that I put it in I get this error
LINK : error LNK2020: unresolved token (0A000019) punchPro
LINK : fatal error LNK1120: 1 unresolved externals
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11744814
No, as I said, you must put it at the top of the cpp, but after the #include that declares it.
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 11744818
JustOneFile.cpp
------------------
// some includes
#include "MyObject.h"      <--- define CMyObject class

CMyObject myInstance;


AnyOtherFile.cpp
-------------------
// some includes
#include "MyObject.h"      <--- define CMyObject class

extern CMyObject myInstance;
0

Featured Post

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

Join & Write a Comment

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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