?
Solved

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

Posted on 2004-08-07
6
Medium Priority
?
134 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

752 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