Making a member callback therough "this"

I

Intiha

Hi,
I was trying to use a member class (B in the example) to update
(basically tell me when something is done- asynchronously!) something
in base class (A in this case).
I was hoping to be able to pass A's "this" pointer so that i can update
values for a specific object via a member object. The following doesnt
work. Is there a better solution on how to do this?

class A;

class B{
B();
~B();
A* ptr;

void set(A* callbackptr){ptr=callbackptr;};

void call(int i){ptr->callback(i);};

};

class A{
public:
A();
~A();
B temp;

void callback(int i){ cout<<"callback with value "<<i<<endl;};

void setCallback(){temp.set(this);};

};


void main(int){

A test;

test.setCallback();
test.temp.call(5);

}
 
I

Intiha

Just to add, I dont want to access this through a global function or a
static function of class A.

Thanks.
 
I

Ian Collins

Intiha said:
Hi,
I was trying to use a member class (B in the example) to update
(basically tell me when something is done- asynchronously!) something
in base class (A in this case).
I was hoping to be able to pass A's "this" pointer so that i can update
values for a specific object via a member object. The following doesnt
work. Is there a better solution on how to do this?
It should if you fix it up to compile!
class A;

class B{ struct B{
B();
~B();
A* ptr;

void set(A* callbackptr){ptr=callbackptr;};

void call(int i){ptr->callback(i);};

void call(int i);
};

class A{
public:
A();
~A();
B temp;

void callback(int i){ cout<<"callback with value "<<i<<endl;};

void setCallback(){temp.set(this);};

};
void B::call(int i){ptr->callback(i);}
void main(int){
What on earth's that?
 
S

sharperdavid

class A;

class B
{
A* ptr;
public:
B(){}
~B(){}

void set( A* callbackptr )
{
ptr = callbackptr;
};

void call( int i )
{
//ptr->callback(i); // Because the A is not defined, so can't call
its member function explictly!
};


};

class A
{
public:
A(){}
~A(){}

void callback( int i )
{
cout << "callback with value "<< i << endl;
};

void setCallback()
{
temp.set(this);
};

B temp;
};
 
I

Intiha

I understand that my code wont compile :) This was just to show
something I wanted to do or if there was some variation with which it
would work.
 
T

tedu

Intiha said:
Hi,
I was trying to use a member class (B in the example) to update
(basically tell me when something is done- asynchronously!) something
in base class (A in this case).
I was hoping to be able to pass A's "this" pointer so that i can update
values for a specific object via a member object. The following doesnt
work. Is there a better solution on how to do this?

class A_ {
public:
virtual void callback(int) = 0;
};

class B {
public:
void set(A_* a) { ptr = a; }
void call(int i) { a->callback(i); }
private:
A_ *ptr;
};

class A : public A_ {
public:
virtual void callback(int);
void setCallback() { temp.set(this); }
private:
B temp;
};
 
S

Sharpdew

Then you can see the following:
class A
{
public:
A(){}
~A(){}

void callback( int i )
{
cout << "callback with value "<< i << endl;
};


void setCallback()
{
temp.set(this);
};


B temp;
};

int main()
{
A test;

test.setCallback();
test.temp.call(5,&A::callback);

return 0;
}

Intiha 写é“:
 
S

Sharpdew

Oh, sorry, the above is not the whole:

class A;
typedef void (A::*CallBack)(int);

class B
{
A* ptr;
public:
B(){}
~B(){}


void set( A* callbackptr )
{
ptr = callbackptr;
};


void call( int i, CallBack cb )
{
//ptr->callback(i); // Because the A is not defined, so
can't call its member function explictly!
(ptr->*cb)(i);
};
};


class A
{
public:
A(){}
~A(){}

void callback( int i )
{
cout << "callback with value "<< i << endl;
};


void setCallback()
{
temp.set(this);
};


B temp;
};

int main()
{
A test;

test.setCallback();
test.temp.call(5,&A::callback);

return 0;
}

Sharpdew 写é“:
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top