CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    May 2003
    Location
    Jodhpur -> Rajasthan -> INDIA
    Posts
    453

    How to pass CMyDoc object or pointer to function of another class called from CMyDoc:

    How to pass CMyDoc object or pointer to function of another class called from CMyDoc::Func()

    Hello

    I want to pass CMyDoc reference or pointer to another class function. How to do this?

    Class CMydoc
    {
    public:
    int i;
    };

    CMyDoc::Func()
    {

    }

    This is class Try and is in different file - Try.cpp:
    Class Try
    {
    FuncTry(CMydoc *pDoc);
    };
    Try::FuncTry(CMydoc *pDoc)
    {
    char cc[10];
    AfxMessageBox(_itoa(cc,pDoc->i,10));
    }
    Leave Your Mark Wherever You Go
    http://www.danasoft.com/sig/d0153030Sig.jpg

  2. #2
    Join Date
    May 2002
    Location
    Romania
    Posts
    929
    CMyDoc::Func()
    {
    //code
    m_TryObj.Try(this);
    //code
    }

    Snakekaa
    Move like a snake, think like a snake, be a snake !!!

  3. #3
    Join Date
    Apr 2000
    Location
    Romania, Cluj-Napoca
    Posts
    57
    There could be several methods depending of the PLACE WHERE YOU CALL THE Try::FuncTry() method.


    1. USING A GLOBAL VARIABLE
    - You define a global variable in the MyDoc.cpp file like this:
    CMyDoc *pMyDoc;

    - In the constructor of the CMyDoc class (file MyDoc.cpp), initialize this global variabile: pMyDoc = this;

    - In the file where you call the method Try::FuncTry() , (this could be anyplace in the code, let's assume the file is

    called xxx.cpp), declare the pMyDoc variable as external like this: extern CMyDoc *pMyDoc.

    - In the file xxx.cpp where u are using the method Try::FuncTry() simply use the global variable pMyDoc:

    FuncTry(pMyDoc);

    WARNING: U SHOULD BE AWARE HOWEVER THAT IF U DESTROY THE DOCUMENT THE ADRESS POINTED BY THE pMyDoc WON'T BE VALID, SO PAY

    ATTENTION !!!

    2. I assume that u are using a SDI application. The following code will work just fine for a SDI app. , but if you understand it u will be able to make it work for MDI app too.
    I also assumed here that u are calling the TryFunc() from a function called OnTest() in class CMainFrame(). The code will also work without problems even if u are calling the TryFunc() in any other place.

    Code:
       // get a reference to the CWinApp object from which CMyApp is derived
       CWinApp *pMainApp = AfxGetApp();
       ASSERT(pMainApp);
    
       // we need this pointer in order to call the GetFirstDocTemplatePosition() function
       POSITION pos = pMainApp->GetFirstDocTemplatePosition();
       CDocTemplate *pDocTemplate = pMainApp->GetNextDocTemplate(pos);
       ASSERT(pDocTemplate);
    
       // at this point we have a reference to the document template, and because we are
       // a SDI application we do not continue the iteration
       pos = pDocTemplate->GetFirstDocPosition();
       CMyDoc *pMyDoc = (CMyDoc *)pDocTemplate->GetNextDoc(pos);
       ASSERT(pMyDoc);
         
       // instantiate our Try object and call the FuncTry() method
       Try objTry;
       objTry.FuncTry(pMyDoc);
    That's all. Tell me if it helped, or rate me, or hate me

  4. #4
    Join Date
    May 2003
    Location
    Jodhpur -> Rajasthan -> INDIA
    Posts
    453
    Query:

    WHy i am getting error when i will put extern CContourAnalyserDoc *pMyDoc; in xxx.h instead of xxx.cpp

    -------------------------
    Hello I am facing problem in passing CMyDoc pointer to another class function.

    It is basically in function defination. I am givig file wise details and where i am facing problem.

    File: CMyData.h
    #include "CMyDoc.h"

    File: CMyData.cpp
    void CMyData::Calc(CMyDoc *pDoc)
    {

    }

    Note: CMyData is polymorphic base class of three other classes.
    So based on pointer available in CMyDoc particular polymorphic subclass's Func function is called.

    File CMyView.cpp

    CMyView::Func()
    {
    CMyData dat;
    CMyDoc *pDoc = this->GetDocument();
    dat.Calc(*pDoc);
    }

    PROB. 1:I am getting syntax error : identifier 'CMyDoc' if i am removing CMyDoc header from my CMyData.h file

    PROB 2.: If i am keeping the "#include "CMyDoc.h" in CMyData.h file then i get following error

    undeclared base class CMyData


    Main Aim: main aim is to pass CMyDoc *p pointer to the class for calculation.


    Any suggestions, help , idea,

    T.I.A.
    Leave Your Mark Wherever You Go
    http://www.danasoft.com/sig/d0153030Sig.jpg

  5. #5
    Join Date
    Feb 2002
    Posts
    3,788
    my suggestion is NOT to use global variables. it's C++ afterall
    see the attached for a more detailed view.
    Attached Files Attached Files

  6. #6
    Join Date
    May 2003
    Location
    Jodhpur -> Rajasthan -> INDIA
    Posts
    453
    Hello

    Thanks for your reply but still my query in second post remains un-answered

    i.e. how to pass CMyDoc's pointer to a function of another class in CMyView.cpp

    2.0 What should be the function signature or defination?
    I tried with following but resulted in error i.e. CMyDoc unresolved identifier even i had included header file of CMyDoc in CMyData

    void Func(CMyDoc *pDoc);

    Plz. reply soon


    TIA
    Leave Your Mark Wherever You Go
    http://www.danasoft.com/sig/d0153030Sig.jpg

  7. #7
    Join Date
    May 2003
    Location
    Jodhpur -> Rajasthan -> INDIA
    Posts
    453

    Exclamation

    Waiting for any kind of help related to this.
    Leave Your Mark Wherever You Go
    http://www.danasoft.com/sig/d0153030Sig.jpg

  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well....the first question would be what you need the pointer for. And what kind of is the other class. It might not be necessary to pass around the document pointer at all depending on your answers to the former questions. Take a look at the following FAQ.

    If you still need to pass the document pointer, you simply use the following signature
    Code:
    void Func(CMyDocument *pPointer)
    {
    }
    'CMyDocument' needs to be the name of your document class you want to pass (or if you are using RTTI you can use the base class 'CDocument' and cast appropriate).

  9. #9
    Join Date
    May 2003
    Location
    Jodhpur -> Rajasthan -> INDIA
    Posts
    453
    Thanks Andreas

    i will try to implement the same and will look after this. If any problem then i will put a query.
    Thanks again
    Leave Your Mark Wherever You Go
    http://www.danasoft.com/sig/d0153030Sig.jpg

  10. #10
    Join Date
    May 2003
    Location
    Jodhpur -> Rajasthan -> INDIA
    Posts
    453
    HELLO ANDREAS

    I tried to do this fro a class Cxx.cpp to get pointer of CDoc.
    CDocument *doc =AfxGetMainWnd()->GetActiveView()->GetDocument();

    but error came
    error C2039: 'GetActiveView' : is not a member of 'CWnd'

    from MSDN: folowing is the function of CFrameWnd so do i have to use CFrameWnd instead of CWnd ? hOW TO DO THIS?
    Leave Your Mark Wherever You Go
    http://www.danasoft.com/sig/d0153030Sig.jpg

  11. #11
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by d0153030
    HELLO ANDREAS

    I tried to do this fro a class Cxx.cpp to get pointer of CDoc.
    CDocument *doc =AfxGetMainWnd()->GetActiveView()->GetDocument();

    but error came
    error C2039: 'GetActiveView' : is not a member of 'CWnd'

    from MSDN: folowing is the function of CFrameWnd so do i have to use CFrameWnd instead of CWnd ? hOW TO DO THIS?
    Well...you have to cast explicitly here...
    Code:
    CDocument *doc = static_cast<CView*>(static_cast<CMainFrame*>(AfxGetMainWnd())->GetActiveWindow())->GetDocument();
    I have used the base classes 'CView' and 'CMainFrame' in the above sample, they might differ from yours, so you would need to adjust them accordingly. Potentially, you might have to cast the returned document pointer as well...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured