Problem with derived classes 
Author Message
 Problem with derived classes

I'd appreciate it if someone could explain the following behavior and
what to do to fix it :

Class x : public CStatic
{
public:
        x();

protected:
        int value;

public:
        void foo(int val);

Quote:
}

Class y : public x
{
public:
        y();

Quote:
}

x::x()
{
        value = 5;

Quote:
}

void x::foo(int val)
{
        value += val;           //this causes runtime fault in class y

Quote:
}

When I derive y from x,

y myYInst;
y.foo(10);

I get a gpf when the value variable gets updated.  I've tried declaring
it as public and private also, but same result.  I can only update it if
I declare it locally in x::foo(), which doesn't help since I need that
variable accessed by the entire class.

This is just a representation of my classes, but is similar to actual
construction.

any help would be appreciated,

thanks,
   Brian.




Mon, 25 Apr 2005 22:27:43 GMT  
 Problem with derived classes
        Try it in a non MFC environment. ( MFC has a class named CStatic
that may/may not be the issue).

        Also I believe when you wrote
                    y.foo(10);
        you meant
                    myYInst.foo(10);

--
Cheers
Check Abdoul [ VC++ MVP ]
-----------------------------------


Quote:
> I'd appreciate it if someone could explain the following behavior and
> what to do to fix it :

> Class x : public CStatic
> {
> public:
>     x();

> protected:
>     int value;

> public:
>     void foo(int val);
> }

> Class y : public x
> {
> public:
>     y();
> }

> x::x()
> {
>     value = 5;
> }

> void x::foo(int val)
> {
>     value += val;    //this causes runtime fault in class y
> }

> When I derive y from x,

> y myYInst;
> y.foo(10);

> I get a gpf when the value variable gets updated.  I've tried declaring
> it as public and private also, but same result.  I can only update it if
> I declare it locally in x::foo(), which doesn't help since I need that
> variable accessed by the entire class.

> This is just a representation of my classes, but is similar to actual
> construction.

> any help would be appreciated,

> thanks,
>    Brian.





Mon, 25 Apr 2005 23:24:18 GMT  
 Problem with derived classes
I tried removing the CStatic and it fails that way too.  (you're right, I
did mean myYInst.foo(10); )

In case this is the issue, I'm actually trying to manipulate with a DC
object (I pass in CDC *pDC to the function):

        CWnd* myWnd = pDC->GetWindow();
        CRect myRect;
        myWnd->GetClientRect(&myRect);

        myHeight = myRect.Height() - 20;
        myWidth = myRect.Width() - 5;

It fails where I assing myHeight...if commented out, it fails on myWidth.
I know it isn't the myRect element, since I can try it just as
        myHeight = 20;    and this fails also.


Quote:
>         Try it in a non MFC environment. ( MFC has a class named
CStatic
> that may/may not be the issue).

>         Also I believe when you wrote
>                     y.foo(10);
>         you meant
>                     myYInst.foo(10);

> --
> Cheers
> Check Abdoul [ VC++ MVP ]
> -----------------------------------



>> I'd appreciate it if someone could explain the following behavior and
>> what to do to fix it :

>> Class x : public CStatic
>> {
>> public:
>>     x();

>> protected:
>>     int value;

>> public:
>>     void foo(int val);
>> }

>> Class y : public x
>> {
>> public:
>>     y();
>> }

>> x::x()
>> {
>>     value = 5;
>> }

>> void x::foo(int val)
>> {
>>     value += val;    //this causes runtime fault in class y
>> }

>> When I derive y from x,

>> y myYInst;
>> y.foo(10);

>> I get a gpf when the value variable gets updated.  I've tried
declaring
>> it as public and private also, but same result.  I can only update it
if
>> I declare it locally in x::foo(), which doesn't help since I need that
>> variable accessed by the entire class.

>> This is just a representation of my classes, but is similar to actual
>> construction.

>> any help would be appreciated,

>> thanks,
>>    Brian.





Tue, 26 Apr 2005 00:18:05 GMT  
 Problem with derived classes
Looks like your pDC->GetWindow() is failing. Check if the CWnd* returned
equals NULL before you try to use it.

Regards,
Nish [Visual C++ MVP]


Quote:
> I tried removing the CStatic and it fails that way too.  (you're right, I
> did mean myYInst.foo(10); )

> In case this is the issue, I'm actually trying to manipulate with a DC
> object (I pass in CDC *pDC to the function):

> CWnd* myWnd = pDC->GetWindow();
> CRect myRect;
> myWnd->GetClientRect(&myRect);

> myHeight = myRect.Height() - 20;
> myWidth = myRect.Width() - 5;

> It fails where I assing myHeight...if commented out, it fails on myWidth.
> I know it isn't the myRect element, since I can try it just as
>     myHeight = 20;    and this fails also.



> >         Try it in a non MFC environment. ( MFC has a class named
> CStatic
> > that may/may not be the issue).

> >         Also I believe when you wrote
> >                     y.foo(10);
> >         you meant
> >                     myYInst.foo(10);

> > --
> > Cheers
> > Check Abdoul [ VC++ MVP ]
> > -----------------------------------



> >> I'd appreciate it if someone could explain the following behavior and
> >> what to do to fix it :

> >> Class x : public CStatic
> >> {
> >> public:
> >>     x();

> >> protected:
> >>     int value;

> >> public:
> >>     void foo(int val);
> >> }

> >> Class y : public x
> >> {
> >> public:
> >>     y();
> >> }

> >> x::x()
> >> {
> >>     value = 5;
> >> }

> >> void x::foo(int val)
> >> {
> >>     value += val;    //this causes runtime fault in class y
> >> }

> >> When I derive y from x,

> >> y myYInst;
> >> y.foo(10);

> >> I get a gpf when the value variable gets updated.  I've tried
> declaring
> >> it as public and private also, but same result.  I can only update it
> if
> >> I declare it locally in x::foo(), which doesn't help since I need that
> >> variable accessed by the entire class.

> >> This is just a representation of my classes, but is similar to actual
> >> construction.

> >> any help would be appreciated,

> >> thanks,
> >>    Brian.





Tue, 26 Apr 2005 03:10:29 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. New Class derived from Class which derived from CWindowImpl

2. Activating the class wizard on a derived-derived Cwnd class

3. How to Create CCtrlView derived class based on CTreeCtrl derived class

4. How to Create CCtrlView derived class based on CTreeCtrl derived class

5. Class in class OR class derived from class?

6. Class Wizard won't let me derive a class from my class

7. Derived Interface implemented in derived class not found

8. Deriving from own MFC derived class

9. Derive from CDialog derived classes

10. Creating derived dialogue from CDialog-derived class

11. derive class from derived MFC

12. Problem deriving from templated base class...

 

 
Powered by phpBB® Forum Software