473,396 Members | 1,871 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,396 software developers and data experts.

Objects with containers

Neo
I have a class which contains a container type member. Now I want to
know if I need to initialize this container member in the class'
contstructor or is it automatically taken care of.

thanks,
Neo

Dec 22 '06 #1
6 1269

"Neo дµÀ£º
"
I have a class which contains a container type member. Now I want to
know if I need to initialize this container member in the class'
contstructor or is it automatically taken care of.

thanks,
Neo
commonly, you will do nothing in Constructor, but if you wanna enhance
the efficiency of Vector container, please use "reserve" first.

Dec 22 '06 #2
On Dec 22, 8:41 am, "Neo" <zingafri...@yahoo.comwrote:
I have a class which contains a container type member. Now I want to
know if I need to initialize this container member in the class'
contstructor or is it automatically taken care of.
Not generally, no. If you have a pointer to a container you will have
to use new or assign the pointer to somewhere, and this could be done
at initialization. If it's a normal member it will use it's
default-constructor and not initialization is needed, depending on what
you are going to use it for you might want to initialize it though. For
example if you have a vector and know that you'll be inserting 10 000
elements you could initialize it to that size.

--
Erik Wikström

Dec 22 '06 #3

Erik Wikström wrote:
On Dec 22, 8:41 am, "Neo" <zingafri...@yahoo.comwrote:
I have a class which contains a container type member. Now I want to
know if I need to initialize this container member in the class'
contstructor or is it automatically taken care of.

Not generally, no. If you have a pointer to a container you will have
to use new or assign the pointer to somewhere, and this could be done
at initialization. If it's a normal member it will use it's
default-constructor and not initialization is needed, depending on what
you are going to use it for you might want to initialize it though. For
example if you have a vector and know that you'll be inserting 10 000
elements you could initialize it to that size.

--
Erik Wikström
It relies context of course, no one could give a general method.

Dec 22 '06 #4
Neo

Erik Wikström wrote:
On Dec 22, 8:41 am, "Neo" <zingafri...@yahoo.comwrote:
I have a class which contains a container type member. Now I want to
know if I need to initialize this container member in the class'
contstructor or is it automatically taken care of.

Not generally, no. If you have a pointer to a container you will have
to use new or assign the pointer to somewhere, and this could be done
at initialization. If it's a normal member it will use it's
default-constructor and not initialization is needed, depending on what
you are going to use it for you might want to initialize it though. For
example if you have a vector and know that you'll be inserting 10 000
elements you could initialize it to that size.

--
Erik Wikström
No I dont have any pointers but fully instantiated container object and
other objects.

Dec 22 '06 #5

Neo wrote in message ...
>No I dont have any pointers but fully instantiated container object and
other objects.
class game{
public:
game();
game( int screen );
private:
std::vector<std::vector<int grid; // 2D
std::vector<doublevgrid; // will be default inited.
} ;

// init a 10x10 'grid' in the initializer list.
size_t Wi(10), Hi(10);

game::game( int screen ) : grid( Wi,
std::vector<int>( Hi, int( screen ) ) ){ // int() for doc'
// if 'vgrid' were not default constructed, the next line would fail.
vgrid.push_back( 3.1415 );

std::cout << grid.at( 9 ).at( 9 ); // last element
} // game::game(int) Ctor

It isn't 'required'[1], but, it is a C++ sin to not init if you can. <G>

So, now you have the choice.
As Shake Spear said, "To init, or not to init. That is the question....".

Does that answer your question, had you asked one?

[1] - references do.
--
Bob R
POVrookie
Dec 23 '06 #6
commonly, you will do nothing in Constructor, but if you wanna enhance
the efficiency of Vector container, please use "reserve" first.
Actually, it rarely enhances anything...

Mirek

Dec 23 '06 #7

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

Similar topics

14
by: Michael Winter | last post by:
Is the Standard Template Library capable of storing complete objects rather than pointers. For example, would either of the vectors below (a, c) function correctly (note the member in C). class...
9
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
0
by: Stephen Horne | last post by:
I've only very recently started with .NET, and need to use some existing container libraries in new code. I'd like to minimise the amount of porting, but of course these containers are not designed...
21
by: George Exarchakos | last post by:
Hi everyone, I'd like your help... Can we have a std::list<BASEwhere BASE be the base class of a class hierarchy? I want to add to this list objects that are inherited from BASE class but not...
13
by: r.z. | last post by:
I logged construtor and destructor calls for one of my classes and I discovered that the constructor was called only once while the destructor was called 3 times for a single object. I have a...
5
by: Jim | last post by:
Hi, Just wondering which is better vector<record *r; r.push_back(new record(x,y)); or vector<recordr; r.push_back(record(x,y));
3
by: Adrian | last post by:
In the following code example I am trying to create a generic interface for a bunch of objects. Each concrete type is stored in its own container and the a container of pointer's to base is used so I...
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
55
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.