473,385 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Object creation

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
Jul 22 '05 #1
3 1120
"Guriks" <ms*******@gmail.com> wrote in message
news:b4*************************@posting.google.co 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
Jul 22 '05 #2
"Guriks" <ms*******@gmail.com> wrote in message
news:b4*************************@posting.google.co 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
Jul 22 '05 #3
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

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Richard | last post by:
Hi all, I am looking for some help on understanding the overhead associated with object creation in Java. I am writing an application where I have written a class to encapsulate some text...
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
3
by: John Ratliff | last post by:
When I dereference a pointer, does it make a copy of the object? Say I had a singleton, and wanted an static method to retrieve it from the class. class foo { private: static foo *bar; ...
8
by: Anthony Munter | last post by:
I have a web application with impersonate=”true” in Web.config and on my own logon page I allow the user to either - specify a userid/password for the app to impersonate when calling legacy...
3
by: Nick Dreyer | last post by:
I was quite surprised to notice that Sub New() gets called twice, once at declaration time and once at creation time. I can't figure out why it would be called at declaration if there is no class...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
44
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use...
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
0
by: Dhananjay | last post by:
Hi All, I want to develop one application in vb.net for exchange 2000. I tried to add one contact with the code snippet below. The same logic is there for appointment on Microsoft's site. (I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.