Hi,everyone.Because of my English level,I will try to use code to explain
where I confused.
//list of code:
#include <iostream>
#include <algorithm>
#include <vector>
class A{
static int si;
int i;
public:
A():i(si++){}
void show()const{std::cout << i << std::endl;}
};
int A::si(0);
class funA{
public:
void operator ()(A& a){a.show();}
};
int main(){
std::vector<A> veca(10);
std::for_each(veca.begin(),veca.end(),funA());
//system("pause");
}
result:
0
0
0
0
0
0
0
0
0
0
So ,isn't the vector call the constructor function for each member?? 7 1649
"hellwolf" <he*********@hotmail.com> wrote in message
news:ce***********@mail.cn99.com... Hi,everyone.Because of my English level,I will try to use code to explain where I confused.
//list of code: #include <iostream> #include <algorithm> #include <vector>
class A{ static int si; int i; public: A():i(si++){}
Add this copy constructor
A(const A&):i(si++){}
void show()const{std::cout << i << std::endl;} }; int A::si(0);
class funA{ public: void operator ()(A& a){a.show();} };
int main(){ std::vector<A> veca(10); std::for_each(veca.begin(),veca.end(),funA()); //system("pause"); }
result: 0 0 0 0 0 0 0 0 0 0
So ,isn't the vector call the constructor function for each member??
Yes but it calls the copy constructor.
std::vector<A> veca(10);
is the same as
std::vector<A> veca(10, A());
The second parameter A() is copied into each of the vector elements.
john
hellwolf wrote in news:ce***********@mail.cn99.com in comp.lang.c++: So ,isn't the vector call the constructor function for each member??
No it default constructs 1 object and then copy's that object for
each member.
HTH.
Rob.
-- http://www.victim-prime.dsl.pipex.com/
"John Harrison" <jo*************@hotmail.com> дϢ
news:2m************@uni-berlin.de... "hellwolf" <he*********@hotmail.com> wrote in message news:ce***********@mail.cn99.com... Hi,everyone.Because of my English level,I will try to use code to
explain where I confused.
//list of code: #include <iostream> #include <algorithm> #include <vector>
class A{ static int si; int i; public: A():i(si++){}
Add this copy constructor
A(const A&):i(si++){}
void show()const{std::cout << i << std::endl;} }; int A::si(0);
class funA{ public: void operator ()(A& a){a.show();} };
int main(){ std::vector<A> veca(10); std::for_each(veca.begin(),veca.end(),funA()); //system("pause"); }
result: 0 0 0 0 0 0 0 0 0 0
So ,isn't the vector call the constructor function for each member??
Yes but it calls the copy constructor.
std::vector<A> veca(10);
is the same as
std::vector<A> veca(10, A());
The second parameter A() is copied into each of the vector elements.
john
Thank you all , but how can I call the constructor for each member(a for
loop?)
On Tue, 27 Jul 2004 08:19:34 +0800, hellwolf <he*********@hotmail.com>
wrote: "John Harrison" <jo*************@hotmail.com> дÈëÏûÏ¢ news:2m************@uni-berlin.de... "hellwolf" <he*********@hotmail.com> wrote in message news:ce***********@mail.cn99.com... > Hi,everyone.Because of my English level,I will try to use code to explain > where I confused. > > //list of code: > #include <iostream> > #include <algorithm> > #include <vector> > > class A{ > static int si; > int i; > public: > A():i(si++){}
Add this copy constructor
A(const A&):i(si++){}
> void show()const{std::cout << i << std::endl;} > }; > int A::si(0); > > class funA{ > public: > void operator ()(A& a){a.show();} > }; > > int main(){ > std::vector<A> veca(10); > std::for_each(veca.begin(),veca.end(),funA()); > //system("pause"); > } > > > result: > 0 > 0 > 0 > 0 > 0 > 0 > 0 > 0 > 0 > 0 > > So ,isn't the vector call the constructor function for each member?? >
Yes but it calls the copy constructor.
std::vector<A> veca(10);
is the same as
std::vector<A> veca(10, A());
The second parameter A() is copied into each of the vector elements.
john
Thank you all , but how can I call the constructor for each member(a for loop?)
Well you are calling a constructor, it's just not the constructor you
thought it would be.
You could use a for loop
std::vector<A> veca;
for (int i = 0; i < 10; ++i)
veca.push_back(A());
but that looks exactly the same as your old code.
What do you think is wrong with this?
std::vector<A> veca(10);
If you say what you think is wrong, we might be able to help you better.
john
Rob Williscroft <rt*@freenet.co.uk> wrote in message news:<Xn**********************************@130.133 .1.4>... hellwolf wrote in news:ce***********@mail.cn99.com in comp.lang.c++:
So ,isn't the vector call the constructor function for each member??
No it default constructs 1 object and then copy's that object for each member.
HTH.
Rob.
I was trying that, but got more puzzled.
this code:
______________________________
#include <iostream>
#include <algorithm>
#include <vector>
class A{
static int si;
int i;
public:
A():i(si++){}
A(const A& obj):i(si++){}
void show()const{std::cout << i << std::endl;}
};
int A::si(0);
class funA{
public:
void operator ()(A& a){a.show();}
};
int main(){
std::vector<A> veca;
for(int i = 0;i < 10;++i){
veca.push_back(A());
}
std::for_each(veca.begin(),veca.end(),funA());
return 0;
}
___________________________________
gives results
24
25
26
27
28
29
30
31
32
34
But if I remove the copy constructor, I get
0
1
2
3
4
5
6
7
8
9
Please mention why this is happening
Regards
Sandeep
"Sandeep" <sa*************@yahoo.com> wrote in message
news:1b**************************@posting.google.c om... Rob Williscroft <rt*@freenet.co.uk> wrote in message
news:<Xn**********************************@130.133 .1.4>... I was trying that, but got more puzzled. this code: ______________________________ #include <iostream> #include <algorithm> #include <vector>
class A{ static int si; int i; public: A():i(si++){} A(const A& obj):i(si++){} void show()const{std::cout << i << std::endl;} }; int A::si(0);
class funA{ public: void operator ()(A& a){a.show();} };
int main(){ std::vector<A> veca; for(int i = 0;i < 10;++i){ veca.push_back(A()); }
You are default-constructing 10 locally scoped(temporary) A's. push_back
copy constructs each of these ( another 10 ), and the temporaries go out of
scope and are destructed. push_back reallocates space and again copy
constructs an additional 5 A's when there isn't enough space to hold the
additional item. The reallocation strategy may vary with implementations.
std::for_each(veca.begin(),veca.end(),funA()); return 0; } ___________________________________ gives results 24 25 26 27 28 29 30 31 32 34
But if I remove the copy constructor, I get 0 1 2 3 4 5 6 7 8 9
Jeff F
Sandeep wrote in news:1b**************************@posting.google.c om in
comp.lang.c++: Please mention why this is happening
You're not measuring construction and copy-construction
seperatly, when the vector resizes all its elements
are copy-constructed in the new location.
#include <iostream>
#include <algorithm>
#include <vector>
struct A
{
static int si, ci;
int i, j;
A() : i(si++), j(0) {}
A( A const & obj ) : i(obj.i), j(ci++) {}
};
int A::si = 0, A::ci = 0;
struct funA
{
void operator ()(A const & a)
{
std::cout << a.i << " copy " << a.j << '\n';
}
};
int main()
{
std::vector<A> veca;
//veca.reserve( 10 );
for(int i = 0;i < 10;++i)
{
veca.push_back(A());
}
std::for_each( veca.begin(), veca.end(), funA() );
}
I Get:
0 copy 15
1 copy 16
2 copy 17
3 copy 18
4 copy 19
5 copy 20
6 copy 21
7 copy 22
8 copy 23
9 copy 24
But if I uncomment the line:
//veca.reserve( 10 )
in main I get:
0 copy 0
1 copy 1
2 copy 2
3 copy 3
4 copy 4
5 copy 5
6 copy 6
7 copy 7
8 copy 8
9 copy 9
Rob.
-- http://www.victim-prime.dsl.pipex.com/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: cppaddict |
last post by:
Hi,
I am writing a program and needs to know one of its object members
before it can be initialized. It doesn't really matter for my
question (which a C++ question, not a windows question), but...
|
by: cppaddict |
last post by:
Hi,
How, in general, does the default operator= work?
That is, say I have a custom class MyClass and do:
MyClass defaultMyClass;
MyClass initializedMyClass("set","some","members");...
|
by: Michael Jasn |
last post by:
I had a question about memory management. Please look at the two
functions below. Can you answer the two questions in the comments
below. Thanks so much.
-Mike
func1(std::vector<Point>&...
|
by: slurper |
last post by:
i have the following
class sequence {
public:
sequence (const sequence& mysequence, const int newjob) {
job_sequence(mysequence.job_sequence)
job_sequence.push_back(newjob);
...
}
|
by: Alexander Stippler |
last post by:
hi,
the following does not work. I do not understand why, since it works
if I replace line marked by (1) with the line below ((2)). Compiling
with gcc4.0 results in "error: conversion from...
|
by: Someonekicked |
last post by:
In my program, I need to open multiple files, and I wont know till after the
program execution how many of them (user will enter that value). So I am
using a vector of fstream. I am using fstream...
|
by: JoeC |
last post by:
I am still working on my game and my program is getting better. Most
of what I want to works. I think I am having trouble with copy
constructor. Basically I want it to copy the gdata array. My...
|
by: daveb |
last post by:
I'm trying to write some code that calls the constructors of STL
containers explicitly, and I can't get it to compile. A sample program
is below. One compiler complains about the last two lines...
|
by: clicwar |
last post by:
A simple program with operator overloading and copy constructor:
#include <iostream>
#include <string>
using namespace std;
class Vector {
private:
float x,y;
public:
Vector(float u, float...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |