question on vector<T>

S

subramanian100in

Consider the following program:

#include <iostream>
#include <vector>

using namespace std;

class Test
{
public:
Test();
friend ostream &operator <<(ostream &os, const Test &t);
private:
static int counter;
int value;
};

int Test::counter = 0;

Test::Test()
{
value = ++counter;
cout << "from default ctor " << value << endl;
}

ostream &operator <<(ostream &os, const Test &t)
{
os << t.value;
return os;
}

int main()
{
vector<Test> tvec(10);

for (vector<Test>::iterator iter = tvec.begin();
iter != tvec.end();
++iter)
cout << *iter << endl;

return 0;
}

When I compile and run this program under VC++ 2005 Express Edition,
it prints the following

from default ctor 1
1
1
1
1
1
1
1
1
1
1

The default ctor is called only once.I thought default ctor should be
called 10 times because the statement contains tvec(10) as in
vector<Test> tvec(10);
Where am I going wrong.

Also every Test object in tvec has the same value 1. Why is it so ?

Kindly explain.

Thanks
V.Subramanian
 
P

Pete Becker

The default ctor is called only once.I thought default ctor should be
called 10 times because the statement contains tvec(10) as in
vector<Test> tvec(10);
Where am I going wrong.

Also every Test object in tvec has the same value 1. Why is it so ?

It's because the default constructor is only called once. You also need
to instrument the copy constructor.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
N

Naresh Rautela

Consider the following program:

#include <iostream>
#include <vector>

using namespace std;

class Test
{
public:
Test();
friend ostream &operator <<(ostream &os, const Test &t);
private:
static int counter;
int value;

};

int Test::counter = 0;

Test::Test()
{
value = ++counter;
cout << "from default ctor " << value << endl;

}

ostream &operator <<(ostream &os, const Test &t)
{
os << t.value;
return os;

}

int main()
{
vector<Test> tvec(10);

for (vector<Test>::iterator iter = tvec.begin();
iter != tvec.end();
++iter)
cout << *iter << endl;

return 0;

}

When I compile and run this program under VC++ 2005 Express Edition,
it prints the following

from default ctor 1
1
1
1
1
1
1
1
1
1
1

The default ctor is called only once.I thought default ctor should be
called 10 times because the statement contains tvec(10) as in
vector<Test> tvec(10);
Where am I going wrong.

Also every Test object in tvec has the same value 1. Why is it so ?

Kindly explain.

Thanks
V.Subramanian

Try this

Test::Test()
{
value = counter;
cout << "from default ctor " << value << endl;

}

Test::Test(const Test& t)
{
value = ++counter;
cout << "from default copy ctor " << value << endl;

}

Now when you run it, it would output the followng
from default ctor 0
from default copy ctor 1
from default copy ctor 2
from default copy ctor 3
from default copy ctor 4
from default copy ctor 5
from default copy ctor 6
from default copy ctor 7
from default copy ctor 8
from default copy ctor 9
from default copy ctor 10
1
2
3
4
5
6
7
8
9
10

HTH
 
S

subramanian100in

Try this

Test::Test()
{
value = counter;
cout << "from default ctor " << value << endl;

}

Test::Test(const Test& t)
{
value = ++counter;
cout << "from default copy ctor " << value << endl;

}

Now when you run it, it would output the followng
from default ctor 0
from default copy ctor 1
from default copy ctor 2
from default copy ctor 3
from default copy ctor 4
from default copy ctor 5
from default copy ctor 6
from default copy ctor 7
from default copy ctor 8
from default copy ctor 9
from default copy ctor 10
1
2
3
4
5
6
7
8
9
10

HTH
From this I understand that the default ctor is called once and then
the copy ctor is called 10 times. Kindly let me know where this detail
is mentioned - is it defined in some book or defined in the ISO C++98
standard ?

Thanks
V.Subramanian
 
B

BobR

Sarath said:
Why the copy constructor being called only once?

It's much easier to tell what you are doing if you copy your code (only
what's needed) into the post, so we can see it too.

{
vector<Test> tvec(2);
/* You should see something like
Test Ctor // 1
Test Copy Ctor // 2
Test Copy Ctor // 3
Test Dtor // 1
*/
} // vector destructs
/*
Test Dtor // 3
Test Dtor // 2 (or 2, 3 ?)
*/
 
S

Sarath

Why the copy constructor being called only once?

Regards,
Sarath

Sorry for the type in my question, for the elements, why the
constructor only called once?
 
N

Naresh Rautela

the copy ctor is called 10 times. Kindly let me know where this detail
is mentioned - is it defined in some book or defined in the ISO C++98
standard ?

Thanks
V.Subramanian- Hide quoted text -

- Show quoted text -

Hmm. Let me take a shot at explaining what happens behind the scenes.

vector<T> t(n) is actually a constructor of type vector(size_type n,
const T& value = T(), const Allocator& = Allocator())

-The allocator first allocates memory to store the n objects i.e. it
allocates sizeof(A) * n bytes and returns the starting address
-An object is created from the default constructor
-Then the constructor calls _Ufill to copy this object to the memory
location provided by allocator. The copy constructor is used during
this process.
-Once this is done the object created in step 2 is destroyed.

HTH
 
P

*PaN!*

Sorry for the type in my question, for the elements, why the
constructor only called once?

Because the vector's default constructor's parameters are two

vector(int num, T t = T())

The quoted code is equivalent to:

vector<Test> tvec(10, Test());

So the Test's default constructor is called in the constructor's call, then
the copy constructor is used internally in that constructor to copy the
second parameter to each element of the vector.
 
S

subramanian100in

Hmm. Let me take a shot at explaining what happens behind the scenes.

vector<T> t(n) is actually a constructor of type vector(size_type n,
const T& value = T(), const Allocator& = Allocator())

-The allocator first allocates memory to store the n objects i.e. it
allocates sizeof(A) * n bytes and returns the starting address
-An object is created from the default constructor
-Then the constructor calls _Ufill to copy this object to the memory
location provided by allocator. The copy constructor is used during
this process.
-Once this is done the object created in step 2 is destroyed.

HTH


I understood it now. Thank you very much. Kindly let me know the
material(ie book) or internet site where I can find such details about
STL.
 
B

BobR

I understood it now. Thank you very much. Kindly let me know the
material(ie book) or internet site where I can find such details about
STL.

SGI has the (dated) docs to the STL (most of which is now in the 'standard
C++ library').

Dinkumware has more up-to-date references.
http://www.dinkumware.com/manuals/.

"Thinking in C++", vol 2 has some on the 'standard C++ library':
(some minor errors due to changes in the standard after it's (TiCppv2) final
release.)

Get "Thinking in C++", 2nd ed. Volume 1&2 by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top