Question on usage of functional object.

Discussion in 'C++' started by hn.ft.pris@gmail.com, Jan 23, 2007.

  1. Guest

    I've got following code test C++'s functor. For the sake of
    easy-reading, I omit some declearations.



    #include <algorithm>
    #include <functional>

    using namespace std;

    template <typename T> class Sum{
    public:
    Sum(T i=0):sum(i){};
    inline void operator () (T x){
    sum += x;
    }
    inline T output() const{
    return sum;
    }

    private:
    T sum;
    };

    int main( void ){


    vector<int> vec(10, 1);

    Sum<int> sum;

    sum = for_each(vec.begin(), vec.end(), Sum<int>()); .......(1)
    sum = for_each(vec.begin(), vec.end(), sum); .........(2)
    sum = for_each(vec.begin(), vec.end(), sum.operator()(int) ); ...(3)

    cout << sum.output() << endl;

    return 1;
    }

    It's easy to understand that (2) works, because sum.operator()(int) is
    called implicitly. (1) also works, it confuses me. Does Sum<int>()
    create an implicit class Sum object? On the other hand, Sum<int>
    doesn't work.
    (3) fails, does it means the third argument of "for_each" couldn't be a
    function pointer? What will the code be if I want to pass a function
    pointer here? Thanks for help!
    , Jan 23, 2007
    #1
    1. Advertising

  2. Kai-Uwe Bux Guest

    wrote:

    > I've got following code test C++'s functor. For the sake of
    > easy-reading, I omit some declearations.
    >
    >
    >
    > #include <algorithm>
    > #include <functional>
    >
    > using namespace std;
    >
    > template <typename T> class Sum{
    > public:
    > Sum(T i=0):sum(i){};
    > inline void operator () (T x){
    > sum += x;
    > }
    > inline T output() const{
    > return sum;
    > }
    >
    > private:
    > T sum;
    > };
    >
    > int main( void ){
    >
    >
    > vector<int> vec(10, 1);
    >
    > Sum<int> sum;
    >
    > sum = for_each(vec.begin(), vec.end(), Sum<int>()); .......(1)
    > sum = for_each(vec.begin(), vec.end(), sum); .........(2)
    > sum = for_each(vec.begin(), vec.end(), sum.operator()(int) ); ...(3)
    >
    > cout << sum.output() << endl;
    >
    > return 1;
    > }
    >
    > It's easy to understand that (2) works, because sum.operator()(int) is
    > called implicitly. (1) also works, it confuses me. Does Sum<int>()
    > create an implicit class Sum object?


    There is nothing implicit about the function object. Although: it is a
    temporary.

    > On the other hand, Sum<int> doesn't work.


    Right: Sum<int> just denotes a type. It is not an expression that evaluates
    to an object of that type.

    > (3) fails, does it means the third argument of "for_each" couldn't be a
    > function pointer?


    No, it just means you got the syntax wrong.

    > What will the code be if I want to pass a function pointer here?


    Well, just pass a function pointer. Something like:

    void inc_5 ( int & a ) { a+=5; }

    ....

    for_each( vec.begin(), vec.end(), &inc_5 );

    (warning, I just typed it into the newsreader, so there may be typos in the
    code.)

    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Jan 23, 2007
    #2
    1. Advertising

  3. Declaring a dynamic 2D array on the heap

    Hello everyone,

    I'd really appreciate it if anyone could describe some of the nuances
    of declaring and using a dynamic 2D array on the heap.


    Thanx
    <tuShaRsAXEna>
    Tushar Saxena, Jan 23, 2007
    #3
  4. Re: Declaring a dynamic 2D array on the heap

    On Jan 23, 1:25 pm, Tushar Saxena <> wrote:
    > Hello everyone,


    Hi, try to start a new topic next time and don't hijack an existing
    one.

    > I'd really appreciate it if anyone could describe some of the nuances
    > of declaring and using a dynamic 2D array on the heap.


    T* arr = new T[nr];

    Replace T with the type you want an array of and nr with the number of
    elements in the array.

    arr[n]

    Access element n of the array.

    delete[] arr;

    Delete the array when you are done with it.

    Can't think of much more to say about it except that it might be better
    to use std::vector<T> instead.

    std::vector<T> arr = new std::vector<T>();
    delete arr;

    The rest is the same.

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 23, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. metfan
    Replies:
    2
    Views:
    4,847
    Robert Olofsson
    Oct 21, 2003
  2. Colin J. Williams

    Webchecker Usage - a problem with local usage

    Colin J. Williams, Feb 25, 2004, in forum: Python
    Replies:
    1
    Views:
    536
    Colin J. Williams
    Feb 26, 2004
  3. Replies:
    2
    Views:
    272
    Noah Roberts
    Jan 23, 2007
  4. hvt
    Replies:
    0
    Views:
    1,209
  5. hvt
    Replies:
    0
    Views:
    1,469
Loading...

Share This Page