Solved

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

Posted on 2004-08-07
6
132 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DLL Windows equivalent in MAC OS 8 146
add a property to an object 6 135
Microsoft vcsetup.exe and vcssetup.exe 8 450
How can I select directories with in a date range - 14 days. 2 108
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

726 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