473,320 Members | 2,117 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,320 software developers and data experts.

Create a list of class as a member attribute

Hi,

I would like to create a Class which has a list of another class as it
attribute.
I wonder, should I do this:
class B {
private:
vector<A> _listOfA;
}

B::B() {
A a1;
A a2;
_listOfA.push_back(a1);
_listOfA.push_back(a2);
}

or this:

class B {
private:
vector<A*> _listOfA;
}

B::B() {
A* a1 = new A();
A* a2 = new A();
_listOfA.push_back(a1);
_listOfA.push_back(a2);
}

I think #1 is the preferred way in C++. But isn't there is a
performance penalty in #1?
(when you push_back(a1), you are copying a1 to the list of A, right? So
should we do #2, which is pass by reference instead of pass by value?

Jan 19 '06 #1
4 1885
si***************@gmail.com wrote:
I would like to create a Class which has a list of another class as it
attribute.
I wonder, should I do this:
class B {
private:
vector<A> _listOfA;
Provided the compiler knows what 'A' is and what 'vector' is. Also,
consider that naming a _vector_ "list" is potentially confusing.
} ;

B::B() {
A a1;
A a2;
_listOfA.push_back(a1);
_listOfA.push_back(a2);
}

or this:

class B {
private:
vector<A*> _listOfA;
} ;

B::B() {
A* a1 = new A();
A* a2 = new A();
_listOfA.push_back(a1);
_listOfA.push_back(a2);
}

I think #1 is the preferred way in C++. But isn't there is a
performance penalty in #1?
Performance penalty is unrecognisable until the program is complete and
can be run to collect performance data (like under a profiler, for
example).
(when you push_back(a1), you are copying a1 to the list of A, right? So
should we do #2, which is pass by reference instead of pass by value?


Right, you're copying a1. If copying is expensive, storing pointers can
be a better approach. (BTW, in #2 you're copying 'a1' as well, but it is
merely a pointer, so copying of a pointer is usually cheap). Given the
minuscule amount of code you posted, it's impossible to conclude one way
or the other.

Keep in mind that storing pointers obtained from 'new' requires cleaning
them up at the time when they are not needed any longer (in the d-tor, for
example). You've not shown how or where that is done. If it's not done,
you have a "memory leak".

V
Jan 19 '06 #2
si***************@gmail.com wrote:
Hi,

I would like to create a Class which has a list of another class as it
attribute.
I wonder, should I do this:
class B {
private:
vector<A> _listOfA;
}

B::B() {
A a1;
A a2;
_listOfA.push_back(a1);
_listOfA.push_back(a2);
}

or this:

class B {
private:
vector<A*> _listOfA;
}

B::B() {
A* a1 = new A();
A* a2 = new A();
_listOfA.push_back(a1);
_listOfA.push_back(a2);
}

I think #1 is the preferred way in C++. But isn't there is a
performance penalty in #1?
(when you push_back(a1), you are copying a1 to the list of A, right? So
should we do #2, which is pass by reference instead of pass by value?


#1 creates a copy of the object as you said and #2 creates a copy of the
pointer to the object. So, depending on the complexity of the object
there can be a performance hit. I generally use pointers for non-trivial
objects and copies of the object for trivial ones. There is no "right"
way. Both are valid. Of course with the pointer version you need to
delete the objects explicitly.
--dakka
Jan 19 '06 #3
TB
si***************@gmail.com sade:
Hi,

I would like to create a Class which has a list of another class as it
attribute.
I wonder, should I do this:
class B {
private:
vector<A> _listOfA;
}

B::B() {
A a1;
A a2;
_listOfA.push_back(a1);
_listOfA.push_back(a2);
}

or this:

class B {
private:
vector<A*> _listOfA;
}

B::B() {
A* a1 = new A();
A* a2 = new A();
_listOfA.push_back(a1);
_listOfA.push_back(a2);
}

I think #1 is the preferred way in C++.


Don't adhere to an assumed "preferred" way. It actually
boils down to the demands of your design. And the code
you've shown lacks vital context to say which of
the two you should use. But as a question in general, see
my first sentence.

--
TB @ SWEDEN
Jan 19 '06 #4
si***************@gmail.com wrote:
Hi,

I would like to create a Class which has a list of another class as it
attribute.
I wonder, should I do this:
class B {
private:
vector<A> _listOfA;
}

B::B() {
A a1;
A a2;
_listOfA.push_back(a1);
_listOfA.push_back(a2);
}

or this:

class B {
private:
vector<A*> _listOfA;
}

B::B() {
A* a1 = new A();
A* a2 = new A();
_listOfA.push_back(a1);
_listOfA.push_back(a2);
}


I recommend you avoid using pointers in general, unless you're sure you
need it.
Make sure you do a performance test that shows you'll need this added
complexity.
Sometimes having to call new, actually makes your code less efficient,
because some libraries have poor implementation for new.

If you do have to use pointers, then I recommend you use smart pointers
in your container, instead of raw pointers.
You can use the boost::shared_ptr or the following cow_ptr
http://code.axter.com/cow_ptr.h

Jan 19 '06 #5

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

Similar topics

2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
3
by: Tony Johansson | last post by:
Hello!! Hello Experts! I'm right if I say that if I have attribute that is constant or attribute that is of reference type or attribute that is class type then I must use initialize list. ...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
3
by: Tapas | last post by:
Hi, Generating a .cs file using CodeDom. It generates the class fine. But i have few queries about class generation. 1. How to create a protected member? By default it generates a private...
1
by: Gürkan Demirci | last post by:
Hi, i am using the VisualStudio FormDesigner to create an asp:table. I want to populate an asp:tablecell with different controls at runtime. In the codebehind file, there is an attribute for the...
6
by: ahart | last post by:
I'm pretty new to python and am trying to write a fairly small application to learn more about the language. I'm noticing some unexpected behavior in using lists in some classes to hold child...
18
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
9
by: glue | last post by:
I have a class with a list member and the list seems to behave like it's static while other class members don't. The code... class A: name = "" data = def __init__(self, name): self.name =...
10
by: Henrik Dahl | last post by:
Hello! I have an xml schema which has a date typed attribute. I have used xsd.exe to create a class library for XmlSerializer. The result of XmlSerializer.Serialize(...) should be passed as the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.