Advertisement

Pointers and classes

Started by January 18, 2001 01:30 PM
1 comment, last by FrigidHelix 24 years ago
I am occasionally getting ''Access Violations'' in my app, so I was wandering if the following is legal (I use stuff like it all the time):
  
// Not actual code. Just to give idea


class obj1
{
protected:
   int data;
public:
   function1();
};

class obj2
{
protected:
   obj1  data;
public:
   obj1* GetData(); // returns pointer data

}


Function2()
{
   obj2 myobj;
   myobj.GetData()->function1();
}
  
Basically, what concerns me, is knowing if it is legitimate to access a pointer to protected data. All my stuff works, but now I seem to be getting these nasty debug failures when running in debug mode ''Access Violation 0xC000005''. (not sure on the digit).
There''s nothing inherently wrong with what you''re doing.

The AV is happening because you''re trying to use a pointer that doesn''t point to a valid object. Probably you forgot to initialize it, or you''re trying to use it after it''s been deleted.

-Mike
Advertisement
It is valid to do so. What isn''t valid is to continue using that pointer after the instance has been destroyed.
Keys to success: Ability, ambition and opportunity.
Ok. Thanks for the info.

This topic is closed to new replies.

Advertisement