On Thu, Jun 19, 2008 at 10:09 AM, frank zhu <[EMAIL PROTECTED]> wrote:
> Thanks for both of your answers. However, as a beginner :-) , I don't get
> your points yet .
> Let me elaborate a little more about my two questions,
> 1. Class A has a public member function - print. Why is it wrong for Class
> B , which is not a subclass of class A, to include class A as its private
> member ?
It's not wrong, of course you can do that, but you can only use A
internally, you can longer do b.pa->print inside main() since pa is
private. You will have to wrap that call in a public method (assuming
you create an object for *a to point to)
b::print() {
a->print();
}
> 2. When the following program runs, class B object instantiates . However,
> the pointer pa is not initialized, therefore, it can point anywhere. Even
> point to NULL. However, statement
> b1.pa->print();
> never fails to execute. Why?
It's not something to rely on. You should create a new A for *pa to point to:
a* pa = new A; //better yet, do this with the constructor
-- Brett
------------------------------------------------------------
"In the rhythm of music a secret is hidden;
If I were to divulge it, it would overturn the world."
-- Jelaleddin Rumi