Object creation

G

Guriks

HI,
When can i use aggregation and composition. I mean

Class A{

};

Class B{

private:
A a;
A* a;
}

In the abovecode. When can i use A a and A* a?

Regards
 
M

Mike Wahler

Guriks said:
HI,
When can i use aggregation and composition.

Whenever you like. :)
I mean

Class A{

};

Class B{

private:
A a;
A* a;

You can't have two data members with the same name.
}
};


In the abovecode. When can i use A a and A* a?

Private members of a class are only accessible by
member functions of the same class, and by friends
of that class.

Which C++ book(s) are you reading?

-Mike
 
I

Ivan Vecerina

Guriks said:
HI,
When can i use aggregation and composition. I mean

Class A{

};

Class B{

private:
A a;
A* a;
}

In the abovecode. When can i use A a and A* a?

You should use a direct data member ( A a ), unless
there is a good reason to use a pointer (e.g. the
member is not always present, or is polymorphic,
or you want to implement a special C++ idiom such
as the 'pimpl'/implementation hiding).

When a pointer/indirection is needed, not that in
most cases a smart pointer (std::auto_ptr<A> or other)
will be preferred to a naked pointer (A*).
This helps ensure proper resource management.

hth,
Ivan
 
E

EventHelix.com

I assume you are asking if A should be contained or referenced as a
pointer.

Directly adding "a" as a member variable makes the design simple as
you do not need to worry about allocating memory for a, it is allocated
within b.
The disadvantage here is that you have anybody using the class B has to
directly or indirectly include the header file containing class A.

Deepa
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,323
Members
48,008
Latest member
KieraMcGuf

Latest Threads

Top