| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"stef" |
| Date: |
05 Jan 2008 05:47:52 AM |
| Object: |
vector init behavior |
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...
.
|
|
| User: "Andrew Koenig" |
|
| Title: Re: vector init behavior |
05 Jan 2008 10:23:11 AM |
|
|
"stef" <stef.pellegrino@gmail.com> wrote in message
news:026243bb-71be-4908-b3d7-46f6797de3a9@q77g2000hsh.googlegroups.com...
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.
.
|
|
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: vector init behavior |
05 Jan 2008 06:03:36 AM |
|
|
* 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));
This should not compile since you have not included <vector>.
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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
|
|
|
|
| User: "Pete Becker" |
|
| Title: Re: vector init behavior |
05 Jan 2008 06:00:58 AM |
|
|
On 2008-01-05 06:47:52 -0500, stef <stef.pellegrino@gmail.com> said:
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.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
.
|
|
|
|
| User: "Daniel T." |
|
| Title: Re: vector init behavior |
05 Jan 2008 12:04:16 PM |
|
|
stef <stef.pellegrino@gmail.com> wrote:
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.
.
|
|
|
|

|
Related Articles |
|
|