Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Object creation

Reply
Thread Tools

Object creation

 
 
Guriks
Guest
Posts: n/a
 
      01-23-2005
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
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      01-23-2005
"Guriks" <> wrote in message
news: m...
> 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


 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      01-23-2005
"Guriks" <> wrote in message
news: m...
> 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com


 
Reply With Quote
 
EventHelix.com
Guest
Posts: n/a
 
      01-24-2005
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
--
http://www.EventHelix.com/EventStudio
EventStudio 2.5 - Automate sequence diagram generation

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to (batch) set EXIF date taken and IPTC creation date and creation time for photos with filenames YYMMDDHHMMSS#.jpg? guercheLE@gmail.com Digital Photography 1 10-04-2005 07:15 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Dynamic creation of object problem Harry F. Harrison ASP .Net 1 08-11-2004 12:49 AM
COM object creation fails when impersonation is done [PSC] =?Utf-8?B?QW50aG9ueSBNdW50ZXI=?= ASP .Net 8 05-12-2004 07:21 AM
Which is advisable for object creation? Balaji Kannan ASP .Net 3 12-26-2003 12:35 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57