开发者

copy constructor problem while reading an object directly into vector using copy and stream iterators

开发者 https://www.devze.com 2023-01-25 20:20 出处:网络
can some body please explain the behavior in the output while running the following code. I am little confused about number of times the copy constructor getting called.

can some body please explain the behavior in the output while running the following code. I am little confused about number of times the copy constructor getting called.

using namespace std;
class A {
    int i;
public:
    A() {
    };
    A(const A& a) {
        i = a.i;
        cout << "copy constructor invoked" << endl;
    };
    A(int num) {
        i = num;
    };
    A& operator = (const A&a) {
        i = a.i;
//      cout << "assignment operator invoked" << endl;
    };
    ~A() {
        cout << "destructor called" << endl;
    };
    friend ostream& operator << (ostream & out, const A& a);
    friend istream& operator >> (istream &in, A&a);
};

ostream & operator << (ostream &out, const A& a) {
    out << a.i;
    return out;
}

istream & operator >> (istream & in, A&a) {
    in >> a.i;
    return in;

}
int main() {
    vector<A开发者_JAVA百科> vA;
    copy(istream_iterator<A>(cin), istream_iterator<A>(), back_inserter(vA));
//  copy(vA.begin(), vA.end(), ostream_iterator<A>(cout, "\t"));
    return 0;
}

The output observed is


ajay@ubuntu:~/workspace/ostream_iterator/src$ ./a.out 
40
copy constructor invoked
copy constructor invoked
copy constructor invoked
copy constructor invoked
copy constructor invoked
copy constructor invoked
copy constructor invoked
copy constructor invoked
copy constructor invoked
copy constructor invoked
copy constructor invoked
destructor called
destructor called
destructor called
destructor called
destructor called
destructor called
destructor called
destructor called
destructor called
destructor called
destructor called
destructor called
destructor called
ajay@ubuntu:~/workspace/ostream_iterator/src$ 


I thought the copy constructor would be called once while inserting into the vector, as containers stores objects by values.


The istream_iterator is getting built and copied a bunch of times. Here's my output:

40
copy constructor invoked
copy constructor invoked
copy constructor invoked
copy constructor invoked
destructor called
destructor called
destructor called
destructor called
copy constructor invoked
copy constructor invoked
destructor called
copy constructor invoked
copy constructor invoked
destructor called
copy constructor invoked
copy constructor invoked
copy constructor invoked
copy constructor invoked
copy constructor invoked
copy constructor invoked
copy constructor invoked


The copy constructor should be called at least 3 times:

  • 1 copy per istream_iterator passed to std::copy, as istream_iterator stores a local value of the parameterized type and most likely copies it in its copy constructor and they are passed by value to std::copy. (2)
  • 1 to insert into vector<A> vA via vA.push_back(...), called by the back_inserter(vA) assignment operator. (3)

The compiler can probably optimize out a few of those copies, but where the other 8 come from is a mystery to me.

EDIT: Incidentally, I get 8 calls to the copy constructor running on codepad.org: http://codepad.org/THFGFCCk

EDIT2: When I ran this with VS2008, I got 7 calls to the copy constructor. The additional 4 were the result of copying around the input iterators to std::copy for bounds checking in a debug build. When running in a fully optimized build, I only get 3 calls to the copy constructor.

0

精彩评论

暂无评论...
验证码 换一张
取 消