Okay thanks, geirha. I have been playing with this some more and it makes for an interesting demo on pointers.
These two statements are equivalent:
Code:
par.child->parent->quad();
par.child->parent->child->parent->child->parent->child->parent->child->parent->child->parent->child->parent->quad();
I wonder what kind of performance hit the second statement takes
Code:
class A;
class B
{
public:
A* parent;
B(A* );
void print();
//Hi refs nothing so it is fine here
void hi()
{ cout << "hi" << endl;}
};
class A
{
public:
int qqq;
double* ddd;
B* child;
A()
{
cout << "Class A constructor: " << this << endl;
qqq = 32;
child = new B(this);
//Create a gig of data in ram to prove that it is not duplicated
ddd = new double[(int)1.25e8];
for ( int x = 0; x < (int)1.25e8; x++ )
ddd[x] = x;
}
//quad refs nothing so it is fine here
void quad()
{
qqq *= 4;
}
};
B::B(A* p)
{
cout << "Class B constructor: " << this << endl;
parent = p;
cout << "Parent = : " << parent << endl;
}
//Print References A therefore it needs to be after A
void B::print()
{
cout << parent->qqq << endl;
}
int main(void)
{
A par;
par.child->print();
par.child->parent->quad();
//par A creates child B passing it A's address
//B refs A which has function quad
par.child->parent->child->parent->child->parent->child->parent->child->parent->child->parent->child->parent->quad(); //Same function as above
par.child->print();
par.child->hi();
char c; cin >> c; //Pause
}
Bookmarks