Click here to Skip to main content
15,881,413 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How can I treat VT_DATE ? Pin
CPallini14-Apr-11 0:15
mveCPallini14-Apr-11 0:15 
GeneralRe: How can I treat VT_DATE ? Pin
_Flaviu14-Apr-11 0:26
_Flaviu14-Apr-11 0:26 
AnswerRe: How can I treat VT_DATE ? Pin
T210215-Apr-11 10:47
T210215-Apr-11 10:47 
QuestionHow to use One Pointer Object in another .cpp file? Pin
002comp13-Apr-11 23:44
002comp13-Apr-11 23:44 
AnswerRe: How to use One Pointer Object in another .cpp file? Pin
«_Superman_»14-Apr-11 0:44
professional«_Superman_»14-Apr-11 0:44 
GeneralRe: How to use One Pointer Object in another .cpp file? [modified] Pin
002comp14-Apr-11 1:35
002comp14-Apr-11 1:35 
AnswerRe: How to use One Pointer Object in another .cpp file? Pin
Albert Holguin15-Apr-11 4:35
professionalAlbert Holguin15-Apr-11 4:35 
AnswerRe: How to use One Pointer Object in another .cpp file? Pin
Stefan_Lang14-Apr-11 7:02
Stefan_Lang14-Apr-11 7:02 
What is the flow of your program? Or more to the point, what is the lifecycle of your TEST object? In C/C++, when you create an object, then you have to make sure that it will be properly destroyed as well. In this case, since you call new to create your TEST object, you have to make sure that you destroy it again, using delete. Normally you ensure this either by calling delete in the same function where you call new, or, if the object is a member of another class, you call delete in the destructor of this class.

Now we come to your actual question: How can I use this object in another cpp file? I will ask back: How do you call the code in that other cpp file? (And more: how do you execute that code in A.cpp that creates your TEST object?) You somehow have to tell the computer to execute that code, and the only way to do that is by calling a function from that file. You say you don't have a header for that file though - but how then can you call any function from here?

The answer is, you will at some point, somewhere, call a function in your other cpp file - and that is assuming that wherever you call this function from, in that file you must define the prototype for the function that you call! If not by including a header (that you say does not exist), then by manually copying the prototype here.

When you do that call, just add the pointer to the parameter list of that function. That's all. Below is some sample code - I've made a few assumptions about what you did, but made sure it will work this way:
// file A.h
class TEST {
public: // <-- you need this if you want to directly access num elsewhere (not a good style, but thats another problem)
   TEST(); // you said you initialized num in constructor, so you must have this!
   int num;
   void fun();
}
// you said the code that creates your TEST object is in A.cpp, but since you normally don't
// create an object of a class type from within that class I added a function to do that below
TEST* createTest();
// since I added a Creator, I added the appropriate destructor as well here:
void destroyTest(TEST* pObj);  

// file A.cpp
#include "A.h"
TEST::TEST() : num(0) {} // constructor initializes num with 0
void TEST::fun() {}
TEST* createTest() {
  TEST* pObj = new TEST();
  pObj->num++;
  return pObj; // this makes sure you can access the new object from elsewhere
}
void destroyTest(TEST* pObj) { // this is needed to properly destroy the object again!
   delete pObj;
}

// file Sample.cpp
#include "A.h"
void theUnknownFunction(TEST* p) { // pass your object as a parameter like this
   int mynumber = p->num; // read num from object
}

// file main.cpp
#include "A.h"
void theUnknownFunction(TEST* p); // it would be better to put this in a header file!

int main() {
   TEST* pObj = createTest();
   theUnknownFunction(pObj); // now you pass the object that you created to your function in sample.cpp
   destroyTest(pObj); // don't forget to clean up again
}

AnswerRe: How to use One Pointer Object in another .cpp file? Pin
Albert Holguin14-Apr-11 14:43
professionalAlbert Holguin14-Apr-11 14:43 
GeneralRe: How to use One Pointer Object in another .cpp file? [modified] Pin
002comp14-Apr-11 21:14
002comp14-Apr-11 21:14 
GeneralRe: How to use One Pointer Object in another .cpp file? Pin
Stefan_Lang14-Apr-11 23:18
Stefan_Lang14-Apr-11 23:18 
GeneralRe: How to use One Pointer Object in another .cpp file? Pin
002comp15-Apr-11 2:24
002comp15-Apr-11 2:24 
GeneralRe: How to use One Pointer Object in another .cpp file? Pin
Stefan_Lang15-Apr-11 4:42
Stefan_Lang15-Apr-11 4:42 
GeneralRe: How to use One Pointer Object in another .cpp file? Pin
Albert Holguin15-Apr-11 4:29
professionalAlbert Holguin15-Apr-11 4:29 
AnswerRe: How to use One Pointer Object in another .cpp file? Pin
Code-o-mat14-Apr-11 23:07
Code-o-mat14-Apr-11 23:07 
QuestionHow to convert filename to fully qualified name when running an app with a filename argument in the current working directory Pin
Erik13-Apr-11 20:05
Erik13-Apr-11 20:05 
AnswerRe: How to convert filename to fully qualified name when running an app with a filename argument in the current working directory Pin
«_Superman_»13-Apr-11 21:30
professional«_Superman_»13-Apr-11 21:30 
GeneralRe: How to convert filename to fully qualified name when running an app with a filename argument in the current working directory Pin
Erik14-Apr-11 0:00
Erik14-Apr-11 0:00 
GeneralCMFCEditBrowseCtrl leaks... Pin
danLenehan13-Apr-11 8:51
danLenehan13-Apr-11 8:51 
GeneralRe: CMFCEditBrowseCtrl leaks... Pin
Maximilien13-Apr-11 9:42
Maximilien13-Apr-11 9:42 
GeneralRe: CMFCEditBrowseCtrl leaks... Pin
danLenehan13-Apr-11 13:48
danLenehan13-Apr-11 13:48 
GeneralRe: CMFCEditBrowseCtrl leaks... Pin
Maximilien13-Apr-11 14:15
Maximilien13-Apr-11 14:15 
QuestionTried Searching but Can't find the Answer Pin
JohnnyG13-Apr-11 8:20
JohnnyG13-Apr-11 8:20 
AnswerRe: Tried Searching but Can't find the Answer Pin
Albert Holguin13-Apr-11 8:27
professionalAlbert Holguin13-Apr-11 8:27 
GeneralRe: Tried Searching but Can't find the Answer Pin
JohnnyG13-Apr-11 8:39
JohnnyG13-Apr-11 8:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.