Click here to Skip to main content
15,921,660 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Vector VS. Array BUG Pin
Steve Echols25-Mar-08 20:02
Steve Echols25-Mar-08 20:02 
GeneralRe: Vector VS. Array BUG Pin
Stephen Hewitt25-Mar-08 20:05
Stephen Hewitt25-Mar-08 20:05 
GeneralMessage Removed Pin
25-Mar-08 20:06
Armond Sarkisian25-Mar-08 20:06 
GeneralRe: Vector VS. Array BUG Pin
Stephen Hewitt25-Mar-08 20:10
Stephen Hewitt25-Mar-08 20:10 
GeneralRe: Vector VS. Array BUG Pin
Steve Echols25-Mar-08 19:58
Steve Echols25-Mar-08 19:58 
GeneralMessage Removed Pin
25-Mar-08 20:04
Armond Sarkisian25-Mar-08 20:04 
GeneralRe: Vector VS. Array BUG Pin
Steve Echols25-Mar-08 20:20
Steve Echols25-Mar-08 20:20 
GeneralRe: Vector VS. Array BUG Pin
Rajasekharan Vengalil25-Mar-08 20:06
Rajasekharan Vengalil25-Mar-08 20:06 
That's an interesting question! See if this code helps answer it.

C++
#include <iostream>
#include <vector>
#include <stdlib.h>

using namespace std;

class Foo {
private:
  int num;
public:
  Foo() {
    num = rand();
    cout<<"Foo::Foo() - num = "<<num<<endl;
  }
  
  explicit Foo(const Foo& f) {
    num = f.num;
    cout<<"Foo::Foo(const Foo&) - num = "<<num<<endl;
  }
  
  const Foo& operator=(const Foo& f) {
    cout<<"Foo::operator=()"<<endl;
    num = f.num;
    return *this;
  }
  
  int getnum() {
    return num;
  }
};

int main() {
  vector<Foo> v(5);
  cout<<"size = "<<v.size()<<endl;
  cout<<"capacity = "<<v.capacity()<<endl;

  vector<Foo>::iterator it = v.begin();
  while( it != v.end() )
    cout<<(*it++).getnum()<<endl;
  return 0;
}
This is the output I get on VC++ 2008:

Foo::Foo() - num = 41
Foo::Foo(const Foo&) - num = 41
Foo::Foo(const Foo&) - num = 41
Foo::Foo(const Foo&) - num = 41
Foo::Foo(const Foo&) - num = 41
Foo::Foo(const Foo&) - num = 41
size = 5
capacity = 5
41
41
41
41
41
As you can see, the copy constructor gets called 5 times! So the compiler first creates a temporary "Foo" instance and then copies it 5 times.

--
gleat
http://blogorama.nerdworks.in[^]
--

Once we figured out that taking our shoes and socks off would double our counting ability the technical glitch was quickly rectified.

-- Chris Maunder, from the CP newsletter

GeneralRe: Vector VS. Array BUG Pin
Stephen Hewitt25-Mar-08 20:09
Stephen Hewitt25-Mar-08 20:09 
GeneralMessage Removed Pin
25-Mar-08 20:11
Armond Sarkisian25-Mar-08 20:11 
GeneralRe: Vector VS. Array BUG Pin
Stephen Hewitt25-Mar-08 20:16
Stephen Hewitt25-Mar-08 20:16 
GeneralRe: Vector VS. Array BUG Pin
Steve Echols25-Mar-08 20:14
Steve Echols25-Mar-08 20:14 
GeneralRe: Vector VS. Array BUG Pin
Stephen Hewitt25-Mar-08 20:21
Stephen Hewitt25-Mar-08 20:21 
GeneralRe: Vector VS. Array BUG Pin
Steve Echols25-Mar-08 20:31
Steve Echols25-Mar-08 20:31 
GeneralOn Dialog Box Pin
Chandrasekharan P25-Mar-08 18:20
Chandrasekharan P25-Mar-08 18:20 
GeneralRe: On Dialog Box [modified] Pin
rp_suman25-Mar-08 19:29
rp_suman25-Mar-08 19:29 
GeneralRe: On Dialog Box Pin
Chandrasekharan P25-Mar-08 19:56
Chandrasekharan P25-Mar-08 19:56 
AnswerRe: On Dialog Box Pin
Rajkumar R25-Mar-08 19:45
Rajkumar R25-Mar-08 19:45 
GeneralRe: On Dialog Box Pin
Chandrasekharan P25-Mar-08 20:25
Chandrasekharan P25-Mar-08 20:25 
GeneralRe: On Dialog Box [modified] Pin
Rajkumar R25-Mar-08 21:22
Rajkumar R25-Mar-08 21:22 
GeneralRe: On Dialog Box Pin
rp_suman25-Mar-08 19:55
rp_suman25-Mar-08 19:55 
GeneralRe: On Dialog Box Pin
Chandrasekharan P25-Mar-08 20:07
Chandrasekharan P25-Mar-08 20:07 
GeneralRedirection in command line "&lt;" Pin
uusheikh25-Mar-08 17:30
uusheikh25-Mar-08 17:30 
GeneralRe: Redirection in command line "&lt;" Pin
User 58385225-Mar-08 17:59
User 58385225-Mar-08 17:59 
GeneralRe: Redirection in command line "&lt;" Pin
fefe.wyx25-Mar-08 18:21
fefe.wyx25-Mar-08 18:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.