Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-08-07
6
Medium Priority
?
135 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
[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
  • 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 1000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

597 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