vector init behavior

S

stef

Hello,

Under VS2005 with a console projet:


class Foo
{
private:
static int x;

public:

Foo(int i, int j)
{
cout << "pass number : " << x++ << endl;
}
};


int Foo::x=1;


//-----
void main()
{
vector<Foo> a(10, Foo(5,7));
return;
}



Basically, I suppose that the main vector init will enter ten times
into the constructor of Foo.
Obviously I'm totally wrong, because executing this just gives me only
"pass number: 1" on screen

Conclusion: It seems that vector starts creating only one instance of
the Foo class then "malloc the others"
(9 times in my case)


Why does it do that ? performance ?


Thanks for your help...
 
P

Pete Becker

Foo(int i, int j)
{
cout << "pass number : " << x++ << endl;
}

Basically, I suppose that the main vector init will enter ten times
into the constructor of Foo.
Obviously I'm totally wrong, because executing this just gives me only
"pass number: 1" on screen

Conclusion: It seems that vector starts creating only one instance of
the Foo class then "malloc the others"
(9 times in my case)


Why does it do that ? performance ?

Read the specification for vector's constructor. Then instrument Foo's
copy constructor.
 
A

Alf P. Steinbach

* stef:
Under VS2005 with a console projet:

Yes, it's a good idea to state compiler etc. In this case, it doesn't
matter much, except for...

class Foo
{
private:
static int x;

public:

Foo(int i, int j)
{
cout << "pass number : " << x++ << endl;
}
};


int Foo::x=1;


//-----
void main()

This would not be accepted by a standard-conforming compiler.

"main" must always have result type "int".

{
vector<Foo> a(10, Foo(5,7));

return;

This is unnecessary and anyway should not compile (however, apparently
Visual C++ 2005 accepts "void main", which if so is non-conforming).

}



Basically, I suppose that the main vector init will enter ten times
into the constructor of Foo.
Obviously I'm totally wrong, because executing this just gives me only
"pass number: 1" on screen

Conclusion: It seems that vector starts creating only one instance of
the Foo class then "malloc the others"
(9 times in my case)


Why does it do that ? performance ?

You have explicitly asked for a given Foo instance to be copied as the
default value for the vector's elements.

Copying that instance does not call your user-defined constructor.


Cheers, & hth.,

- Alf
 
A

Andrew Koenig

vector<Foo> a(10, Foo(5,7));
Basically, I suppose that the main vector init will enter ten times
into the constructor of Foo.

Eleven times: Once to construct Foo(5,7) and then ten calls to the copy
constructor to make ten copies of that object for the elements of the
vector.
 
D

Daniel T.

stef said:
class Foo
{
private:
static int x;

public:

Foo(int i, int j)
{
cout << "pass number : " << x++ << endl;
}
};


int Foo::x=1;


//-----
void main()
{
vector<Foo> a(10, Foo(5,7));
return;
}



Basically, I suppose that the main vector init will enter ten times
into the constructor of Foo.

Wrong. The vector constructor puts 10 copies of Foo(5, 7) into itself.
Conclusion: It seems that vector starts creating only one instance of
the Foo class then "malloc the others"

No it doesn't malloc them, it uses the (default) copy constructor to
make copies of the one you provided, in this case, it makes 10 copies.
The one you provided (a temporary) is then discarded.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top