Please comment on the code

S

sci

For B to call back a member of A, the code is design as:

class A;

class B
{
A* p;
public:
void Init(void* m) { p = (A*)m; }
void docallback();
};


class A
{
B b;
public:
A() { b.Init(this); }
void dowork() { }

};


void B::docallback()
{
((A*)p)->dowork();
}

Thanks!
 
K

Kevin Goodsell

sci said:
For B to call back a member of A, the code is design as:

class A;

class B
{
A* p;
public:
void Init(void* m) { p = (A*)m; }

C-style casts are evil. Why didn't you just make Init take a A*?
void docallback();
};


class A
{
B b;
public:
A() { b.Init(this); }
void dowork() { }

};


void B::docallback()
{
((A*)p)->dowork();

Why are you casting p to the type it already is?

-Kevin
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top