Click here to Skip to main content
16,016,204 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: converting file to bits Pin
digwizfox2-Jul-04 11:02
digwizfox2-Jul-04 11:02 
GeneralRe: converting file to bits Pin
John R. Shaw3-Jul-04 13:53
John R. Shaw3-Jul-04 13:53 
GeneralCreate PDF Pin
doctorpi2-Jul-04 7:46
doctorpi2-Jul-04 7:46 
GeneralRe: Create PDF Pin
Antti Keskinen2-Jul-04 8:38
Antti Keskinen2-Jul-04 8:38 
GeneralRe: Create PDF Pin
doctorpi2-Jul-04 8:41
doctorpi2-Jul-04 8:41 
GeneralRe: Create PDF Pin
Henry miller2-Jul-04 9:40
Henry miller2-Jul-04 9:40 
GeneralGetDocument missunderstood Pin
JabraJabra2-Jul-04 7:16
JabraJabra2-Jul-04 7:16 
GeneralRe: GetDocument missunderstood Pin
Antti Keskinen2-Jul-04 8:25
Antti Keskinen2-Jul-04 8:25 
You are using GetDocument correctly, but you are misunderstanding the concepts of how object-oriented C++ and MFC work. To fill in this gap, consider this example:

You have a class called CDocument, that represents the layout (design) of a document class. When you use the new operator or declare an object on the stack, an instance of the class is created. All standard data members of this class belong to this very instance. If you declare a new instance of the class, the new instance will have it's own data members.

What is happening in your code is that you have two CFormView classes which use the same CDocument class. But, when the CFormView objects are created, they both create seperate instances of the CDocument class. This means that though you have told both form views to use the same document class, the actual document objects created from these classes are different objects, both with their own variables. Changing one variable does not change the other one. In short, the form views do not share the document object: both views haver their own instance of the document.

However, there is an option to achieve what you desire in C++. It is called the static keyword. Determine the variable myVar as static in the class declaration. Now, each instance of the CDocument class will share the variable. Here is a code example to clarify:
#include <iostream>

using namespace std;<DIV>

class CDoc
{
public:
	CDoc() {}
	~CDoc() {}<DIV>

public:
	static int nIndex;
};<DIV>

// This declaration is required, as the static variable is considered FILE SCOPE
int CDoc::nIndex;<DIV>

int main(void)
{
	CDoc* ptrDoc1 = new CDoc();
	ptrDoc1->nIndex = 2;<DIV>

	cout << "The contents of the static variable is " << ptrDoc1->nIndex << endl;<DIV>

	CDoc* ptrDoc2 = new CDoc();
	ptrDoc2->nIndex = 60;<DIV>

	cout << "The static variable was altered and became " << ptrDoc1->nIndex << endl;<DIV>

	delete ptrDoc1; ptrDoc1 = NULL;
	delete ptrDoc2; ptrDoc2 = NULL;<DIV>

	return 0;
}<DIV>

The output is:
The contents of the static variable is 2
The static variable was altered and became 60
I hope this will clarify the idea.

Additionally, The MFC Framework offers it's very own style to implement the idea 'multiple views for a single document'. You should follow the example set forth in this MSDN link to implement similar functionality. Actually, I suggest you follow only the MSDN example, as I am not perfectly sure the static variable approach would work in MFC. But hopefully the huge code paste was useful Smile | :)

-Antti Keskinen

----------------------------------------------
The definition of impossible is strictly dependant
on what we think is possible.
Generalthanks! Pin
JabraJabra2-Jul-04 8:36
JabraJabra2-Jul-04 8:36 
GeneralAutoFill Pin
Anonymous2-Jul-04 6:19
Anonymous2-Jul-04 6:19 
GeneralRe: AutoFill Pin
palbano2-Jul-04 8:09
palbano2-Jul-04 8:09 
GeneralRe: AutoFill Pin
Anonymous2-Jul-04 8:27
Anonymous2-Jul-04 8:27 
GeneralRe: AutoFill Pin
palbano2-Jul-04 8:45
palbano2-Jul-04 8:45 
QuestionInfinite loop ?? Pin
Spank me!!2-Jul-04 6:08
Spank me!!2-Jul-04 6:08 
AnswerRe: Infinite loop ?? Pin
zarzor2-Jul-04 6:43
zarzor2-Jul-04 6:43 
AnswerRe: Infinite loop ?? Pin
Henry miller2-Jul-04 7:28
Henry miller2-Jul-04 7:28 
AnswerRe: Infinite loop ?? Pin
Tom Wright2-Jul-04 9:09
Tom Wright2-Jul-04 9:09 
AnswerRe: Infinite loop ?? Pin
gamitech2-Jul-04 9:42
gamitech2-Jul-04 9:42 
Questionhow to customize the 'Cookie' http header of Internet Explorer Pin
Leo Harrison2-Jul-04 5:24
Leo Harrison2-Jul-04 5:24 
GeneralPatch Pin
Dennis L2-Jul-04 4:42
Dennis L2-Jul-04 4:42 
GeneralRe: Patch Pin
David Crow2-Jul-04 4:46
David Crow2-Jul-04 4:46 
GeneralRe: Patch Pin
palbano2-Jul-04 8:47
palbano2-Jul-04 8:47 
Question&quot;.&quot; formated IP Adress string from hostent? Pin
george ivanov2-Jul-04 3:52
george ivanov2-Jul-04 3:52 
AnswerRe: &quot;.&quot; formated IP Adress string from hostent? Pin
David Crow2-Jul-04 4:45
David Crow2-Jul-04 4:45 
GeneralRe: &quot;.&quot; formated IP Adress string from hostent? Pin
george ivanov9-Jul-04 3:35
george ivanov9-Jul-04 3:35 

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.