473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

constructor and destructor when initializing sized vector

When I run this simple program:

#include <vector>
#include <iostream>
using namespace std;

class A {
public:
A() { cout << "[ctor]" << endl; }
~A() { cout << "[dtor]" << endl; }

};

int main()
{
vector<A> v(3);
return 0;
}

I've got the following result:

[ctor]
[dtor]
[dtor]
[dtor]
[dtor]

(gcc 3.3.4)

How to explain that ? Should not the constructor be called 3 times and
the destructor also ?

Thanks.
Jul 22 '05 #1
2 1397
jjleto wrote:
When I run this simple program:

#include <vector>
#include <iostream>
using namespace std;

class A {
public:
A() { cout << "[ctor]" << endl; }
~A() { cout << "[dtor]" << endl; } Add:
A(const A&) { cout << "[copy ctor]" << endl; }
A& operator=(const A&) { cout << "[op=]" <<endl; return *this; }
int main()
{
vector<A> v(3)


The constructor you are using above has a second defaulted argument
which is the value to fill the vector with. It is defaulted to a default
constructoed A object.

So you get:
ctor - default constructed object for second constructor parameter
copy ctor - v[0] being filled
copy ctor - v[1] being filled
copy ctor - v[2] being filled.
Jul 22 '05 #2
Ron Natalie a écrit :
jjleto wrote:
When I run this simple program:

#include <vector>
#include <iostream>
using namespace std;

class A {
public:
A() { cout << "[ctor]" << endl; }
~A() { cout << "[dtor]" << endl; }


Add:
A(const A&) { cout << "[copy ctor]" << endl; }
A& operator=(const A&) { cout << "[op=]" <<endl; return *this; }
int main()
{
vector<A> v(3)

The constructor you are using above has a second defaulted argument
which is the value to fill the vector with. It is defaulted to a default
constructoed A object.

So you get:
ctor - default constructed object for second constructor parameter
copy ctor - v[0] being filled
copy ctor - v[1] being filled
copy ctor - v[2] being filled.


Thanks. It is clear now.
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: PengYu.UT | last post by:
The following program calls the normal constructor and the copy constructor. By calling the copy constuctor is redundandant, all I want is only a vector of a trial object. Is there any way to...
4
by: fatgirl.brown | last post by:
Hi all, I am attempting to initialize a vector of a vector in a constructor with some clean-looking syntax and am not sure of how to go about this. For instance: double weight; vector<...
3
by: yancheng.cheok | last post by:
Hello all, I try to figure out what is the sequence when we create and delete an object. After experiment on my VC++ 2003, I found that here is the sequence new-constructor-destructor-delete ...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.