CDocument non-default constructor? 
Author Message
 CDocument non-default constructor?

Hi,
I would like to have my CDocument-derived classes (in my applications)
accept parameters in their constructors (and eliminate the default
constructor).

Unfortunately, however, CDocument-derived objects are not created
directly by the user (i.e. me, the programmer), but rather created
using serialization by the document's template. This basically forces
the use of default constructors only for CDocument-derived classes -
unless... someone has an idea how to do that (it's like eating the cake
and still having it :-))

Any idea?

TIA,
Swengtoo

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Fri, 04 Apr 2003 03:00:00 GMT  
 CDocument non-default constructor?

If you want to construct your document with parameters to the constructor, you
will have to derive a class from the doc template class (CSingleDocTemplate or
CMultiDocTemplate), and override its OpenDocumentFile method to call the
constructor with the parameters you'd like.  However, I am very curious where
these arguments will come from.  What variable data will you have to pass at the
time the document is constructed?



Fri, 04 Apr 2003 03:00:00 GMT  
 CDocument non-default constructor?

Quote:

> constructor with the parameters you'd like.  However, I am very
curious where
> these arguments will come from.  What variable data will you have to
pass at the
> time the document is constructed?

Thank you very much for your tip. Answering your question, I would like
to pass "global", application-specific objects to the document. For
example, suppose my application interfaces multiple instances of a
hardware device that provide data to the document, but these device
interfaces are also controlled/accessed from other modules of the
application.
Conceptually, instantiating them belongs in the CWinApp-derived module
(InitInstance) or the CMainFrame module (OnCreate), but then you would
have to #include "winappderived.h" in each and every module that uses
those interface objects. This is not a good modular design: If I want
to use my CDocument-derived class in a different application, I
can't...

Besides, Scott Meyers advises in his book "More Effective C++" to avoid
as much as possible (it's not always possible) default constructors for
various reasons (see item #4 in the book)

Swengtoo.

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 05 Apr 2003 03:00:00 GMT  
 CDocument non-default constructor?

Quote:

> If you want to construct your document with parameters to the
> constructor, you will have to derive a class from the doc template
> class (CSingleDocTemplate or CMultiDocTemplate), and override its
> OpenDocumentFile method to call the constructor with the parameters
> you'd like.

I just looked at the source for CMultiDocTemplate::OpenDocumentFile()
(my application is MDI) and realized that achieving what I want to
accomplish will require some (dangerous?) tricks and will most
certainly break upward compatibility with future versions of MFC. The
reason? MFC's CDocument creation method doesn't leave easy room for
constructors other than the default. OpenDocumentFile() uses
CDocTemplate::CreateNewDocument() which in turn calls m_pDocClass-
Quote:
>CreateObject() to create the document. How can I pass parameters to a

constructor that way? I see no easy way...

Is there a way to accomplish a reusable CDocument-derived class without
requiring its constructor to have parameters?

Thanks again,
Swengtoo.

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 05 Apr 2003 03:00:00 GMT  
 CDocument non-default constructor?

Quote:

> Is there a way to accomplish a reusable CDocument-derived class
> withoutrequiring its constructor to have parameters?

Well, after mining the web, I managed to find an idea how to do that
(credits: a posting from Shekar Narayanan in the MFC Programmer's
SourceBook Discussion Board):

======== START QUOTE =========
The OpenDocumentFile(NULL) will return a pointer to CDocument. Do
whatever you want to do with this pointer and call the document's
UpdateAllView(...) function. In the view, instead of doing you initial
processing in the OnInitialUpdate(), do it in OnUpdate(). Remember,
OnInitialUpdate calls OnUpdate. So you have do some checking in the
OnUpdate() function - something like

if (GetDocument()->m_bNotInitialized)
  return;
========== END QUOTE ==========

Any comments (or other ideas)?

Swengtoo

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 05 Apr 2003 03:00:00 GMT  
 CDocument non-default constructor?
I am new to this newsgroup so I am starting at the oldest messages first, so
I apologize for being late.

I put a breakpoint on a CDocument constructor and looked at the call stack.
It looks like the actual construction (new operator) is in code generated by
IMPLEMENT_DYNCREATE, so you might be able to re-define it.

Alternatively, would it work to use a static member to pass parameters? That
is also a flaky solution, but I think that MFC is not designed as well as it
could be.

Quote:



> > If you want to construct your document with parameters to the
> > constructor, you will have to derive a class from the doc template
> > class (CSingleDocTemplate or CMultiDocTemplate), and override its
> > OpenDocumentFile method to call the constructor with the parameters
> > you'd like.

> I just looked at the source for CMultiDocTemplate::OpenDocumentFile()
> (my application is MDI) and realized that achieving what I want to
> accomplish will require some (dangerous?) tricks and will most
> certainly break upward compatibility with future versions of MFC. The
> reason? MFC's CDocument creation method doesn't leave easy room for
> constructors other than the default. OpenDocumentFile() uses
> CDocTemplate::CreateNewDocument() which in turn calls m_pDocClass-
> >CreateObject() to create the document. How can I pass parameters to a
> constructor that way? I see no easy way...

> Is there a way to accomplish a reusable CDocument-derived class without
> requiring its constructor to have parameters?

> Thanks again,
> Swengtoo.

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Wed, 21 May 2003 03:00:00 GMT  
 CDocument non-default constructor?
MFC is complex.
If you want to create CDocument and pass parameters to it you have to derive
class from CDocTemplate(CSingleDocTemplate, CMultiDocTemplate)

or like you says "uses static is the simplest way".



Tue, 17 Jun 2003 20:13:10 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Non-default constructors for COM objects

2. Non Default Constructors Called From Definition Classes

3. Member variable with non-default constructor?

4. Opening non CDocument data file into CDocument...

5. Calling Default Constructor from another constructor

6. Can you call a default constructor from an overloaded constructor

7. HELP: vc 1.52 - close app in CDocument Constructor

8. GetDocString Error when called in CDocument constructor

9. Calling CDocument::OpenNewDocument crashing in non afx_msg call

10. Missing calls to default constructor in unmanaged VC++ 7.0

11. CDialog getting CWnd reference through default constructor?

12. class, instance, and default constructors

 

 
Powered by phpBB® Forum Software