Hi,
Using vc++ 2003, I have an unexpected behaviour:
the following statement uses the copy-ctor to create
the vector items while I expected it uses the default-ctor:
std::vector<Foo> vFoo(10);
Am I wrong?
Marco.
// Full test program
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
class Foo
{
int m_iVal;
public:
Foo();
Foo(const Foo & copy);
void Print();
};
Foo::Foo()
{
static int i = 0;
++i;
m_iVal = i;
}
Foo::Foo(const Foo & copy)
{
static int i = 100;
++i;
m_iVal = i;
}
void Foo::Print()
{
std::cout << m_iVal << std::endl;
}
int main()
{
std::vector<Foo> vI(10);
std::for_each(vI.begin(), vI.end(), std::mem_fun_ref(&Foo::Print));
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
marcosegurini
|
7/21/2003 8:44:50 PM |
|
"marco_segurini" <marcosegurini@virgilio.it> wrote in message news:a33fe984.0307202248.1eee8ef1@posting.google.com...
> Hi,
>
> Using vc++ 2003, I have an unexpected behaviour:
> the following statement uses the copy-ctor to create
> the vector items while I expected it uses the default-ctor:
>
> std::vector<Foo> vFoo(10);
>
> Am I wrong?
Yes. The ten elements are filled by copying the second initializer argument.
The constructor you are using is essentially:
vector(size_type n, const T& value = T())
The second arg in your case is defaulted to a default constructed Foo that is copied
ten times.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Ron
|
7/22/2003 12:43:41 AM
|
|
marco_segurini wrote:
> Using vc++ 2003, I have an unexpected behaviour:
> the following statement uses the copy-ctor to create
> the vector items while I expected it uses the default-ctor:
>
> std::vector<Foo> vFoo(10);
The signature of this constructor is actually
template < class T, class Alloc = std::allocator<T> >
vector<T, Alloc>::vector( size_type n, const T& val = T(),
const Alloc& a = Alloc() );
So it default constructs one Foo (using the second
parameter's default argument) and copies it ten times.
--
Richard Smith
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Richard
|
7/23/2003 11:25:41 AM
|
|
"Ron Natalie" <ron@sensor.com> wrote in message news:3f1c531d$0$39702
> The constructor you are using is essentially:
> vector(size_type n, const T& value = T())
>
> The second arg in your case is defaulted to a default constructed Foo that
is copied
> ten times.
Good point. Class vector can be used to store objects with no default
constructor.
struct Foo {
explicit Foo(int);
};
int main() {
vector<Foo> v(10, Foo(10));
v.push_back(Foo(11));
}
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Siemel
|
7/23/2003 11:14:05 PM
|
|