•[Bar.H]
class Bar {
public:
Bar();
Bar(int a);
void
myFunction(); // this method would be defined
// elsewhere
(e.g. in Bar.C)
protected:
int m_a;
};
Bar::Bar(){ m_a = 0;}
Bar::Bar(int a){ m_a = a;}
[main.C]
#include
"Bar.H"
int main(int argc, char *argv[])
{ // declare a pointer to
Bar; no memory for a Bar instance is
// allocated now p currently points to garbage
Bar *
p;
{ // create a new instance of the class
Bar (*p)
// store pointer to this
instance in p
p = new
Bar();
if (p == 0) {
// memory allocation failed
return 1;
}
}
// since
Bar is in global storage, we can still call methods on it
// this
method call will be successful
p->myFunction();
}