Answered I want to ask one more time issue about ctor, dtor, STL

  • Thursday, August 09, 2012 12:59 AM
     
     
    #include <vector>
    #include <functional>
    #include <algorithm>
    #include <iostream>

    using namespace std;

    class greaterthanX: public binary_function<int, int, bool>
    {
    private:
    int* count;
    public:
    greaterthanX() : count(new int(0))
    {
    cout << "constructor" << endl;
    }
    greaterthanX(const greaterthanX& other)
    {
    cout << "copy constructor" << endl;
    count = new int;
    *count = *other.count;
    }
        result_type operator()(first_argument_type i, second_argument_type j) const
        {
    cout << i << " " << j << " count :" << (*count)++ << endl;
    cout << "count's address : " << count << endl;
            return (result_type)(i > j);
        }
    ~greaterthanX()
    {
    cout << "destructor" << endl;
    }
    greaterthanX &operator=(const greaterthanX &other)
    {
    if(this != &other)
    {
    delete count;
    count = new int;
    *count = *other.count;
    }
    return *this;
    }
    };

    void main()
    {
    vector<int> v1;
    vector<int>::iterator Iter;

    int i;
    for (i = 0; i <= 5; i++)
    {
    v1.push_back(5 * i);
    }

    cout << "The vector v1 = ( " ;
    for (Iter = v1.begin(); Iter != v1.end(); Iter++)
    {
    cout << *Iter << " ";
    }
    cout << ")" << endl;

    // Compare: counting the number of integers > 15 in the vector
    // with a user defined function object
    vector<int>::iterator::difference_type result1b;
    /*result1b = count_if(v1.begin(), v1.end(), greaterthan5());*/
    result1b = count_if(v1.begin(), v1.end(), bind2nd(greaterthanX(), 4));
    cout << "The number of elements in v1 greater than 15 is: " << result1b << "." << endl;
    }

    this is my code and result screen.

    I asked the same question and I arranged the code.

    previously,answerer said the problem is absence of copy ctor and operator=.

    So I complemented those.

    But there are several thing I don't understand.

    first, although I made a copy ctor, count point the same memory.

    copy ctor 's role is for preventing this but not works well.

    second, as you see, greaterthanX should be called six times because vector contains six elements.

    but the number of ctor + copy ctor and dtor was called three times respectively.

    something wrong in my code?

    I have no way to solve this mystery. T.T

All Replies

  • Thursday, August 09, 2012 12:37 PM
     
     Answered

    Hi,

    not sure I understand the question.

    Please note that copy constructor called when a new object is initialized from the object of the same type.

    And there is no need to create a copy of 'greaterthanX' object on every iteration.

    1) ctor -- Initially, you create an object of type greaterthanX.

    2) copy ctor -- an object of type greaterthanX passed to bind2nd, a copy is made

    3) copy ctor -- a copy of binder is made during count_if invocation

    • Marked As Answer by stillwhere Friday, August 10, 2012 12:13 AM
    •  
  • Thursday, August 09, 2012 1:58 PM
     
     Answered

    And to elaborate on Grigoriy's comment.  Once the third copy is made, it is used 6 times (once for every element in the vector), just like you would expect.  Then count_if completes, and the three temporary copies of your object are destroyed.  The final copy used by count_if is reused 6 times (which is what you want, otherwise you would have six copies of your object and *count would never go beyond 1.

    I would like to point out that you made your functor unnecessarily complicated, unless you are exploring object lifetimes as an academic exercise.  There is no reason to use new at all to allocate an integer.  Instead of an int*, just make count an int.  Then you can safely omit the destructor, assignment operator and copy constructor.

    Also, if you are pasting code into a question on these forums, please use the [Insert Code Block] button.  It makes the code much easier to read.

    • Marked As Answer by stillwhere Friday, August 10, 2012 12:13 AM
    •