Default constructor is called immediately?

Hey guys,

This may be a C++ thing I just don't understand, but here is my issue. I have a template class I made called List<T>. I want to use it inside another class (ClassA) and I define it like so in my header:

 
List<SomeObject> objects;


Then, I need to call a specific constructor of List<T> so i can set its size.

So in my ClassA constructor, I call it:

1
2
3
4
5
6
7
8
9
ClassA::ClassA(int _size)
{
//Why the hell is my list's default constructor called right here?

objects = List<T>(_size);
//That didn't work. Why is objects initialized with the default constructor?
//I saw it go through in debug, but the member is not updated?

}


What am I missing here?

Thanks.

~Sebastian
Initializer lists

1
2
3
4
ClassA::ClassA(int _size)
  : objects(_size)
{
}


If you fail to explictily call a ctor in the initializer list, the default ctor is used.

The body of the ctor (the braces) is not exectured until after everything in the initializer list is constructed.
Last edited on
Oh man... Ok, I'll have to remember that from now on. I'm just so used to C# and its elegance that things in C++ seem so backwards sometimes. :P Thanks for your help!
Last edited on
Topic archived. No new replies allowed.