By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,137 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,137 IT Pros & Developers. It's quick & easy.

Objects with containers

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a

"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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.