Click to See Complete Forum and Search --> : Sharing Class Instance between Views in SDI environment ?


Prasutagus
April 24th, 1999, 10:29 PM
This may be a simple one : I created a multi view SDI environment with a custom class. Since Instances of this class need tobe accessible from within each view I declared them in CDoc (regular declaration 'CSkills m_Skills;') but cannot access any of the member function from the views. Funny enough when I start typing 'm_Skills.' the correct list of member functions is displayed but compiling it creates an error message stating that 'm_Skills' is an undeclared identifier.

Should place the declartion somewhere else ? I tried going back to CMainFrm causing a number of Link errors (duplicate declaration of m_Skills in object CDoc ?!?!).

Thank you for your suggestions.

Pras

tchung
April 27th, 1999, 02:05 AM
maybe , I think
You shoud include your header file in order.
for example, You need to include your header file 'myview.h' for CMyView Class. But if CMyView Class has a member-function called GetDocument(),
you should also include a header-file 'mydoc.h' for CMyDoc class .
So,
#include "mydoc.h"
#include "myview.h"

never do this
#include "myview.h"
#include "mydoc.h"

Is that your answer? or I am very sorry.


by nfuox

Dave Lorde
April 27th, 1999, 09:39 AM
To directly access the member variables of another class instance (e.g. your document), they must be declared public in that class.

From the view, you can then access them by casting the return value of GetDocument() to your own derived document type:

#include "MyDocument.h"
...
MyDocument* pMyDoc = dynamic_cast<MyDocument*>(GetDocument());
CSkills localSkills = pMyDoc->m_Skills;

It may be simpler to change the GetDocument() function in your derived view to return a pointer to your derived document:

MyDocument* MyView::GetDocument()
{
return dynamic_cast<MyDocument*>(CView::GetDocument());
}

Be aware that there is an inline release mode version of GetDocument() in the view header file, as well as the out of line debug version.

Dave