A question regarding passing a variable from CView to CDocument
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: A question regarding passing a variable from CView to CDocument

  1. #1
    Join Date
    Jul 2005
    Posts
    940

    A question regarding passing a variable from CView to CDocument

    I am attaching a sample project. Basically what this project does is that I initialize a variable map<CString, bool*> m_mapTest in CView::OnInitialUpdate and then pass the variable to CDocument class. On the other hand, within the function OnSaveDocument defined in CDocument I check m_mapTest. To my surprise, the CString part of m_mapTest is correct but the bool* part of m_mapTest is wrong(I pass an array of bool in CView::OnInitialUpdate). Could any guru here point out why? Thanks.
    Attached Files Attached Files
    Last edited by LarryChen; December 12th, 2012 at 04:44 PM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,384

    Re: A question regarding passing a variable from CView to CDocument

    Does storing pointers (e.g. bool*) to a document make sense anyway? What happens when you open the document from a different process instance? Do you expect those saved pointer values to be valid for a different process?

    Even worse you are storing a pointers to a temporary object.

  3. #3
    Join Date
    Jul 2005
    Posts
    940

    Re: A question regarding passing a variable from CView to CDocument

    Quote Originally Posted by Arjay View Post
    Does storing pointers (e.g. bool*) to a document make sense anyway? What happens when you open the document from a different process instance? Do you expect those saved pointer values to be valid for a different process?

    Even worse you are storing a pointers to a temporary object.
    I already fixed a problem you mentioned. I am storing a pointer to a object in heap instead of a temporary object. I attach the updated project with the post. If I don't store pointers(bool*), what is the better way to store a bunch of boolean variables? Thank you very much!
    Attached Files Attached Files

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,384

    Re: A question regarding passing a variable from CView to CDocument

    You can store boolean values by storing a bool (i.e. 'bool', not 'bool*'). Again, if you serialize the document to a file, you can't store pointers. It doesn't matter if you are using the heap or not because if the file is opened in another process instance, you'll be only lucky if the pointers work.

    At any rate, there is no need to have the view store anything and many folks make the mistake of storing data in the view and the document. Just store the data in the document and have the view read the data out of the document. That way data is only stored in one place (and any view can access the data if need be).

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center