rahman
rahman

Reputation: 4948

C++ () operator overload

I am a C programmer, Now I am picking up C++ concepts (together with boost library).

please have look at my program and its output:

#include <boost/thread.hpp>
#include <iostream>
using namespace std;
class PowerClass
    {
    private:

        int m, n;

    public:
        double *result;

        PowerClass(int m, int n)
        {
            cout<<"I am in constructor  " << this << endl;
            this->m=m; this->n=n;
            result = new double;
        }

        void operator () ()
        {
            cout<<"I am in ()  " << this << endl;
            *result=m;
            for (int i=1; i<n; ++i)
            {
                *result*=m;
                boost::this_thread::yield();
            }
        }

};
int main()
{
    PowerClass p(2,3);
    cout<<"class created  " <<&p<<endl;
    boost::thread t(p);
    t.join();

}

the out put is:

I am in constructor  0xbfcfe40c
class created  0xbfcfe40c
I am in ()  0x9d0f154

The question could be more silly than plain: I know () operator overloading function will be called upon thread creation, but why do I get a different address for the object (this) in the () operator, than the address of the same object in main() and constructor? is this another copy of the object? why? how may I get work on the same object in () operator overloading function. Thank you

Upvotes: 1

Views: 184

Answers (3)

Loki Astari
Loki Astari

Reputation: 264331

Add the following:

    PowerClass(PowerClass const& copy)
      :  m(copy.m)
      ,  n(copy.n)
      ,  result(new double(*copy.result))
    {
        cout<<"I am COPY constructor  " << this << "  FROM: " << &copy << endl;
    }

This will let you see the copy being made.

PS. You are leaking memory.

Edit: From comments below

I am in constructor 0xbf95cc30 class created 0xbf95cc30
I am COPY constructor 0xbf95cc3c FROM: 0xbf95cc30
I am COPY constructor 0xbf95cc04 FROM: 0xbf95cc3c
I am COPY constructor 0xbf95cb94 FROM: 0xbf95cc04
I am COPY constructor 0x941b184 FROM: 0xbf95cb94
I am in () 0x941b184

Question:
I notice that the I will finally have 2 copies(two object) ok. Any way, why do I get 4 messages of calls to the () overload function?

You don't you get 4 calls to the copy constructor (so there are actually 5 instances of the object around the program). Your object is being copy around inside thread most probably. If you add a print statement to the destructor you will see when they are being destroyed.

This usually happens when you don't have optimized tuned on (the optimizer can eliminate a lot of the default copying).

If you are using g++ then add -O3 If you are using MS tools build in release mode.

Upvotes: 1

Alok Save
Alok Save

Reputation: 206508

You are passing the function parameter(which is your object) by value hence your overloaded function receives a copy of the object.
Of-course, the objects are places at different memory addresses and hence the different address.

You will need to pass the object by Reference to receive the same object inside your function.

Upvotes: 4

WeaselFox
WeaselFox

Reputation: 7380

Youre passing by value, instead of by reference, so the object is copied. hence the different address.

Upvotes: 1

Related Questions