passing this object

J

Jung, William

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 );
}
 
M

maverik

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);

It's ok, but be careful
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.7
}

// function must take std::string as parameter
CVirtualScreen::setMessage( std::string message )
{
//setMessage to queue}

===================
class Sock : public CSocketComm
{
public:
Sock(CVirtualScreen screen);

Sock(CVirtualScreen * screen);
~Sock();

// QUESTION ///////////////////////////
// DO I DECLARE REFERENCE TO CVirtuaqlScreen LIKE THIS WAY??
CVirtuaqlScreen screen;

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 );

}

Look 9.3.2 (14882:2003) for more info about this pointer.

HTH
 
J

Jung, William

It's ok, but be careful
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.7


Sock(CVirtualScreen * screen);


CVirtuaqlScreen *screen;


Look 9.3.2 (14882:2003) for more info about this pointer.

HTH

I following your pointer syntax but I am still getting error
http://www.oniva.com/upload/1356/e.jpg

any idea what's wrong here?

the line of
void Create1(CVirtualScreen *Vscreen);
AND
CVirtualScreen *Vscreen;

is having problem

===============
Sock::Sock()
{
StartServer();
}

void Sock::Create1(CVirtualScreen *screen)
{
Vscreen = screen;
}

Sock::~Sock()
{
StopComm();
}
===============
class Sock : public CSocketComm
{
public:
Sock();
~Sock();
void Create1(CVirtualScreen *Vscreen);
CVirtualScreen *Vscreen;

};
 
J

Jung, William

The Call to Sock constructor is like this

CVirtualScreen::CVirtualScreen()
{
sock = new Sock();
sock->Create1(this);
}
 
M

maverik

why there is error saying
syntax error : missing ';' before '*'
I already put ; at the end of statement

CVirtuaqlScreen *screen;
^ is this just a typo?

May be you mean
CVirtualScreen *screen;
 
M

maverik

1>d:\snd\remote\remotedemo\app\virtualawear\sock.h(31) : error C2061:
syntax error : identifier 'CVirtualScreen'

CVirtualScreen is unknown identifier in sock.h. From where sock.h
should know what the hell is CVirtualScreen?

Ok. If you included "Sock.h" into the "VirtualScreen.h", you can't
include
"VirtualScreen.h" into "Sock.h" because of circular dependence.

So you can try to write something like:

extern class VirtualScreen;

in Sock.h.
 
J

Jung, William

Ok. If you included "Sock.h" into the "VirtualScreen.h", you can't
include
"VirtualScreen.h" into "Sock.h" because of circular dependence.

So you can try to write something like:

extern class VirtualScreen;

in Sock.h.

Thanks so much your help.

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
 
T

Thomas J. Gritzan

Jung, William write:
Thanks so much your help.

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)

You can't call member function on classes that are only forward
declared. Once you call member functions or create objects of a class,
you need the full declarationn of the class.

So make sure that you include the header for CVirtualScreen in sock.cpp.
Also, don't forget to use "include guards" in your headers.

There are some issues in the FAQ about that point:
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.11
and the two following.

Also, please post the error message you get. You can copy&paste it
directly from the output window. Most people won't click on wild links
in postings.
 
T

Thomas J. Gritzan

===============
Sock::Sock()
{
StartServer();
}

void Sock::Create1(CVirtualScreen *screen)
{
Vscreen = screen;
}

Sock::~Sock()
{
StopComm();
}
===============
class Sock : public CSocketComm
{
public:
Sock();
~Sock();
void Create1(CVirtualScreen *Vscreen);
CVirtualScreen *Vscreen;

};

Why did you remove the CVirtualScreen* from the constructor? If this
class needs a CVirtualScreen for its work, you should pass the pointer
in at the time of creation:

class Sock : public CSocketComm
{
public:
Sock(CVirtualScreen *Vscreen);
~Sock();

private:
CVirtualScreen *Vscreen;
};

Sock::Sock(CVirtualScreen *Vscreen)
: Vscreen(Vscreen) // <-- initializer list
{
StartServer();
}

Note also the use of the initializer list. In general, it's preferred in
C++ to initialize variables instead of assignment, when possible.
 
J

Jung, William

Jung, William write:

You can't call member function on classes that are only forward
declared. Once you call member functions or create objects of a class,
you need the full declarationn of the class.

So make sure that you include the header for CVirtualScreen in sock.cpp.
Also, don't forget to use "include guards" in your headers.

What is "include guards" in my headers? any example?
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top