passing this object

Discussion in 'C++' started by Jung, William, Nov 27, 2008.

  1. 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 );
    }
    Jung, William, Nov 27, 2008
    #1
    1. Advertising

  2. Jung, William

    maverik Guest

    On Nov 27, 12:25 pm, "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);


    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
    maverik, Nov 27, 2008
    #2
    1. Advertising

  3. Jung, William

    maverik Guest

    On Nov 27, 1:50 pm, maverik <> wrote:
    > >         screen.setMessage ( message );


    screen->setMessage ( message );
    maverik, Nov 27, 2008
    #3
  4. On Thu, 27 Nov 2008 02:50:01 -0800 (PST), maverik
    <> wrote:

    >On Nov 27, 12:25 pm, "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);

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


    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;

    };
    Jung, William, Nov 27, 2008
    #4
  5. Jung, William, Nov 27, 2008
    #5
  6. The Call to Sock constructor is like this

    CVirtualScreen::CVirtualScreen()
    {
    sock = new Sock();
    sock->Create1(this);
    }
    Jung, William, Nov 27, 2008
    #6
  7. On Thu, 27 Nov 2008 02:53:20 -0800 (PST), maverik
    <> wrote:

    >On Nov 27, 1:50?pm, maverik <> wrote:
    >> > ? ? ? ? screen.setMessage ( message );

    >
    >screen->setMessage ( message );


    why there is error saying
    syntax error : missing ';' before '*'
    I already put ; at the end of statement
    Jung, William, Nov 27, 2008
    #7
  8. Jung, William

    maverik Guest

    On Nov 27, 2:39 pm, "Jung, William" <> wrote:
    > 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;
    maverik, Nov 27, 2008
    #8
  9. Jung, William

    maverik Guest

    On Nov 27, 2:35 pm, "Jung, William" <> wrote:
    > the whole error log is like this:http://www.oniva.com/upload/1356/log.txt


    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?
    maverik, Nov 27, 2008
    #9
  10. Jung, William

    maverik Guest

    On Nov 27, 3:17 pm, maverik <> wrote:
    > On Nov 27, 2:35 pm, "Jung, William" <> wrote:
    >
    > > the whole error log is like this:http://www.oniva.com/upload/1356/log.txt

    >
    > 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.
    maverik, Nov 27, 2008
    #10
  11. On Thu, 27 Nov 2008 04:29:30 -0800 (PST), maverik
    <> wrote:

    >On Nov 27, 3:17?pm, maverik <> wrote:
    >> On Nov 27, 2:35?pm, "Jung, William" <> wrote:
    >>
    >> > the whole error log is like this:http://www.oniva.com/upload/1356/log.txt

    >>
    >> 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.


    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
    Jung, William, Nov 27, 2008
    #11
  12. Jung, William write:
    > On Thu, 27 Nov 2008 04:29:30 -0800 (PST), maverik
    > <> wrote:
    >
    >> On Nov 27, 3:17?pm, maverik <> wrote:
    >>> On Nov 27, 2:35?pm, "Jung, William" <> wrote:
    >>>
    >>>> the whole error log is like this:http://www.oniva.com/upload/1356/log.txt
    >>> 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.

    >
    > 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.

    --
    Thomas
    Thomas J. Gritzan, Nov 27, 2008
    #12
  13. Jung, William wrote:
    > ===============
    > 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.

    --
    Thomas
    Thomas J. Gritzan, Nov 27, 2008
    #13
  14. On Thu, 27 Nov 2008 14:07:33 +0100, "Thomas J. Gritzan"
    <> wrote:

    >Jung, William write:
    >> On Thu, 27 Nov 2008 04:29:30 -0800 (PST), maverik
    >> <> wrote:
    >>
    >>> On Nov 27, 3:17?pm, maverik <> wrote:
    >>>> On Nov 27, 2:35?pm, "Jung, William" <> wrote:
    >>>>
    >>>>> the whole error log is like this:http://www.oniva.com/upload/1356/log.txt
    >>>> 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.

    >>
    >> 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.


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

    >
    >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.
    Jung, William, Nov 28, 2008
    #14
  15. Jung, William wrote:
    >> 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?


    First hit in your favorite search engine might find this:
    http://en.wikipedia.org/wiki/Include_guard

    In short, an include guard avoids multiple inclusion of a header file.
    Example:

    #ifndef SOCK_H_INCLUDED
    #define SOCK_H_INCLUDED

    class Sock
    {
    // ...
    };

    #endif // SOCK_H_INCLUDED

    --
    Thomas
    Thomas J. Gritzan, Nov 28, 2008
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    557
    justanotherguy
    Dec 3, 2004
  2. Phillip Vong
    Replies:
    0
    Views:
    408
    Phillip Vong
    Jul 27, 2006
  3. stefven blonqhern
    Replies:
    3
    Views:
    388
    stefven blonqhern
    Jul 5, 2007
  4. Hal Vaughan
    Replies:
    12
    Views:
    539
    James Connell
    Mar 28, 2008
  5. Andy
    Replies:
    1
    Views:
    272
Loading...

Share This Page