Re: Check on correctness



Thanks for the reply, sheesh what a chump.

Yours...one step closer to being a decent developer.

Tim

www.fruitfruit.com wrote:
I had a problem with CMyView->GetDocument() in release.

Within the constructor of the dialog class, I assigned the attribute,
m_pDoc to the document

ASSERT(m_pDoc = pParent->GetDocument());

In release, my program would throw an exception because m_pDoc was
NULL. I trawled through google, to see this is a common error (also
reading that I should call GetDocument every time rather than assigning
to an attribute).

However, if I assign m_pDoc within the dialog's initialisation list the
attribute is assigned correctly.

CExternalEffectorDlg::CExternalEffectorDlg(CState_SdiView*
pParent,const int AtomIndex)
: CDialog( CExternalEffectorDlg::IDD, pParent ),
m_pDoc( pParent->GetDocument() ),


Is calling a getter in an initialisation list bad practice?

That is due to the fact that ASSERT is ignored in release configuration.
You shall not put initialize code in ASSERT, do it in the following way:
m_pDoc = pParent->GetDocument();
ASSERT(m_pDoc);
This shall work for both Debug and Release.

.



Relevant Pages

  • Re: Check on correctness
    ... Within the constructor of the dialog class, I assigned the attribute, ... my program would throw an exception because m_pDoc was ... reading that I should call GetDocument every time rather than assigning ... That is due to the fact that ASSERT is ignored in release configuration. ...
    (microsoft.public.vc.mfc)
  • Re: defining classes-- different methods
    ... reference to the function assigned to its - constructor - property, ... which is not true of an object assigned to the prototype property of a ... prototype Thus it is wasteful of resources to be assigning function ... not employing closures to emulate private members. ...
    (comp.lang.javascript)
  • Re: "Reconstructing" (Calling the constructor again)
    ... A constructor can only be called once ... /* This dynamically allocates a type 'Foo' object, ... You're *assigning* '*aFoo' a new value. ... initializes the temporary object, it does not affect ...
    (comp.lang.cpp)
  • Re: How to make an object instance available to all members of a page?
    ... Assigning default value is a logical exception from that rule. ... Contract StockContract = new Contract; ... into play because of the Dictionary/Sections properties. ... If you move it to constructor then default value will become null... ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: How to make an object instance available to all members of a page?
    ... Assigning default value is a logical exception from that rule. ... Contract StockContract = new Contract; ... into play because of the Dictionary/Sections properties. ... If you move it to constructor then default value will become null... ...
    (microsoft.public.dotnet.framework.aspnet)

Loading