Q_constructor_how many times is called

hey,
after 8 hours I find no solution !!
I wrote two classes:
1. Book
2. InternetBookStore

1
2
3
4
5
6
7
8
9
10
// InternetBookStore.h
#include<vector>
#include "Book.h"

//=================================
const int MAXBOOKS = 100;
class InternetBookStore{
private:
	vector<Book> _books;
......

1
2
3
4
5
6
7
8
9
10
//InternetBookStore.cpp
#include "InternetBookStore.h" 

using namespace std;
// notice that I used an initializing list so the constructor should be called
InternetBookStore::InternetBookStore(): _books(MAXBOOKS){
	for (int i=0;i<50;++i)
		_books[i].show();
}
........

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// book.h
#ifndef __BOOK_H_INCLUDED__
#define __BOOK_H_INCLUDED__

//=====================================
#include<string>
using namespace std;
class Book{

private:
	string _name;
	string _author;
	int _numCopies;

public:
	Book();
	void show() const;
.....
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Book.cpp
#include <iostream>
#include "Book.h"

Book::Book(){
	_name = _author = "no name";
	_numCopies = 0;
	cout << " Book Ctor ";
}

void Book::show() const{
	cout<<"Name:"<<_name<<" Author:"<<_author<<" Number of copies:"<<_numCopies<<endl;
}
...

and when I call from main--->
1
2
3
4
...
int main(){
InternetBookStore i;
...

now, notice that when the constructor of InternetBookStore called the ctor of Book will be called too from the initialized list, the question how many times?
if the the size of Books' vector is 'MAXBOOKS' then it would be called 'MAXBOOKS' times but when I run the program I see that the constructor called once !!
my output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
./myStore
  Book Ctor
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0
Name:no name Author:no name Number of copies:0


any suggestion?
Based on that code, the constructor will be called 100 times, not once. Post your full code.
Also, never use using directives on global scope in a header. And you're using vector incorrectly. You're supposed to add elements to it when needed, not precreate a whole bunch of dummy objects.
closed account (1vRz3TCk)
Based on that code, the constructor will be called 100 times, not once.

Isn't the constructor called once and the copy constructor call the remaining times?
Ah, you're right. This is true for C++03, which I assume OP is using.

@aasaa: since compilers generally don't use C++11 by default yet, compile your programs using the -std=c++0x parameter.
thanks, the answer was the constructor called once and the copy constructor call the remaining times.
Topic archived. No new replies allowed.