Re: passing this object
On 28 nov, 06:02, "Jung, William" <aopiyy...@yahoo.com> wrote:
Please advice the incorrect area of the following code.
===================
class CVirtualScreen
{
public:
CVirtualScreen();
~CVirtualScreen();
Sock *sock;}
You probably want std::unique_ptr<Sock> instead of Sock* here, of
something similar.
Especially since you didn't define the correct copy constructor and
assignment operator...
boost::scoped_ptr should do for C++03.
===================
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);
'this' is a pointer, for historical reasons.
It would probably be cleaner to pass a reference, in which case
dereference it. (*this)
// function must take std::string as parameter
CVirtualScreen::setMessage( std::string message )
You should probably take that string by const-reference.
class Sock : public CSocketComm
{
public:
Sock(CVirtualScreen screen);
That cannot compile if you pass a pointer; did you even try your code?
And you pass by value, which is nonsense since your virtual screen
shouldn't even be copiable!
Sock(CVirtualScreen& screen);
// QUESTION ///////////////////////////
// DO I DECLARE REFERENCE TO CVirtuaqlScreen LIKE THIS WAY??
CVirtuaqlScreen screen;
No, you don't.
Read a C++ book for the basics.
The syntax to declare a reference to CVirtualScreen is
CVirtualScreen& screen;
Sock::Sock(CVirtualScreen screen)
{
// QUESTION ///////////////////////////
// CAN I SET REFERENCE TO CVirtualScreen LIKE THIS??
// SHOULD I USE POINTER??
screenObject = screen
StartServer();}
Given screenObject was not declared nor defined, that seems difficult.
Assuming your code has been corrected to declare both screen and
screenObject (which is actually your screen member I suppose) as
references, it still wouldn't work. You can only initialize references
in constructor-initializers.
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 );
If screen is a reference, yes, it behaves the same as the actual
value.
But really, it seems like you're trying to code in C++ by guessing
stuff.
Just learn the language.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]