Link to home
Start Free TrialLog in
Avatar of e6694811
e6694811

asked on

classes


 Consider this (easy) following class hierarchy

 CClassA
 {
  protected:
   CMYApplicationDoc *m_doc;
   ......
    (other attributes)
   .....
  public :
    CClassA(CMYApllicationDoc *doc);
   ...
   (other procedures)  
   
};



CClassA::CClassA(CMYApllicationDoc *doc)
{
 m_doc =doc;
}

/*****************************************************/


 CClassB :public CClassA
 {
  protected:  
   ......
    (other attributes)
   .....
  public :
    CClassB(CMYApllicationDoc *doc);
   ...
   (other procedures)  
   
};


CClassB::CClassB(CMYApllicationDoc *doc)
{
  CClassA::CClassA(doc);
}


/*****************************************************/

 CClassC :public CClassB
 {
  protected:  
   ......
    (other attributes)
   .....
  public :
    CClassC(CMYApllicationDoc *doc);
   ...
   (other procedures)  
   
};


CClassC::CClassC(CMYApllicationDoc *doc)
{
  CClassB::CClassB(doc);
  ASSERT( m_doc ==doc);
 /* Here ,assertion fails*/

  ...(some additional code)
}


Does anybody know why the assertion fails ?  Sould'd't m_doc be equal to doc in the end? Pay attention to the constructors ,please.

 Thanks.
Avatar of avinash_sahay
avinash_sahay

No, m_doc will not be equal to doc. In fact, it will be uninitialized. This is because if you change the value of m_doc for one instance of a class, the other instances will not be affected.
ASKER CERTIFIED SOLUTION
Avatar of RONSLOW
RONSLOW

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Of course, same thing for CClassC etc
Avatar of e6694811

ASKER

Answer accepted