inline+virtual

Discussion in 'C++' started by sunil, Aug 26, 2008.

  1. sunil

    sunil Guest

    Hello,
    I have a class deriving from a class that provides ability to
    serialize/deserialize objects over the network. There are two classes
    Requests (sent from client to server) Response(sent from server to
    client), these two classes need to implement a pure virtual method
    called dispatch(), however only client needs to implement
    Response::dispatch() and only server needs to implement
    Request::dispatch(). I am planning to build a library from these two
    classes and client/server will link in this library. The goal is that
    server when linked should not see concrete implementation for
    Response::dispatch() as it will contain calls to client specific
    code , vice versa (will cause bad link errors when building my client/
    server tasks). I am constrained to use the class that provides
    serialization/deserialization infrastructure. The only way I could
    think of getting around this problem is to provide:
    -Request.h that specifies complete interface and Request.cpp that
    provides implementation only for functions common to client/server
    (like getters/setters for formulating req/resp)
    -Request_server.h that includes Request.h and provides inline
    implementation for dispatch() function that contains calls to server
    specific code (included in server)
    -Request_client.h that includes Request.h and provides inline NO-OP
    implementation for dispatch() .
    (included in client)
    However since dispatch() is virtual it cannot be inlined
    (inline=compile time, viritual=runtime orthogonal to each other). The
    only workaround I can think of was to implement dispatch() by calling
    non-virtual function called dispatchRequest(), and implement
    dispatchRequest() as no-op inline function in Request_client.h and as
    acutal function with code in Request_server.h. Is this correct
    solution to the problem?
    Thanks
    sunil, Aug 26, 2008
    #1
    1. Advertising

  2. sunil wrote:

    > I have a class deriving from a class that provides ability to
    > serialize/deserialize objects over the network. There are two classes
    > Requests (sent from client to server) Response(sent from server to
    > client), these two classes need to implement a pure virtual method
    > called dispatch(),


    I doubt that; pull the dispatch() out of the message type objects and
    into something that manages those messages (a message queue on the
    server side, or whatever.)
    Matthias Buelow, Aug 26, 2008
    #2
    1. Advertising

  3. sunil

    sunil Guest

    On Aug 26, 1:56 pm, Matthias Buelow <> wrote:
    > sunil wrote:
    > >  I have a class deriving from a class that provides ability to
    > > serialize/deserialize objects over the network.  There are two classes
    > > Requests (sent from client to server) Response(sent from server to
    > > client), these two classes need to implement a pure virtual method
    > > called dispatch(),

    >
    > I doubt that; pull the dispatch() out of the message type objects and
    > into something that manages those messages (a message queue on the
    > server side, or whatever.)


    I cannot do that since I am using an existing library that provides
    this functionality for serialization of objects
    sunil, Aug 26, 2008
    #3
  4. sunil

    Greg Herlihy Guest

    On Aug 26, 10:03 am, sunil <> wrote:
    >  However since dispatch() is virtual it cannot be inlined
    > (inline=compile time, viritual=runtime orthogonal to each other). The
    > only workaround I can think of was to implement dispatch() by calling
    > non-virtual function called dispatchRequest(), and implement
    > dispatchRequest() as no-op inline function in Request_client.h and as
    > acutal function with code in Request_server.h. Is this correct
    > solution to the problem?


    I don't see any problem that exists here. As you point out, "inline'
    and "virtual" are orthogonal, - meaning that each specifier has no
    effect on the other. In fact, there is nothing in C++ that prevents a
    virtual method from being declared inline, and no reason why a C++
    program should avoid declaring such a method.

    Greg
    Greg Herlihy, Aug 26, 2008
    #4
  5. sunil

    sunil Guest

    On Aug 26, 5:37 pm, Greg Herlihy <> wrote:
    > On Aug 26, 10:03 am, sunil <> wrote:
    >
    > >  However since dispatch() is virtual it cannot be inlined
    > > (inline=compile time, viritual=runtime orthogonal to each other). The
    > > only workaround I can think of was to implement dispatch() by calling
    > > non-virtual function called dispatchRequest(), and implement
    > > dispatchRequest() as no-op inline function in Request_client.h and as
    > > acutal function with code in Request_server.h. Is this correct
    > > solution to the problem?

    >
    > I don't see any problem that exists here. As you point out, "inline'
    > and "virtual" are orthogonal, - meaning that each specifier has no
    > effect on the other. In fact, there is nothing in C++ that prevents a
    > virtual method from being declared inline, and no reason why a C++
    > program should avoid declaring such a method.
    >
    > Greg


    what I mean by orthogonal is that they dont make sense together for
    inlines code get replaced at compile time for virtual the call happens
    at runtime thru virtual table, so if you define a virutal function as
    inline and you try to call it thru base pointer it wont work since
    compiler wouldnt know what function to call (since its inlined, the
    function wont exist in compiled code)
    sunil, Aug 27, 2008
    #5
    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. Abhi
    Replies:
    2
    Views:
    734
    E. Robert Tisdale
    Jul 3, 2003
  2. Alvin
    Replies:
    7
    Views:
    474
    E. Robert Tisdale
    May 6, 2005
  3. Replies:
    3
    Views:
    457
  4. Daniel Vallstrom
    Replies:
    2
    Views:
    1,876
    Kevin Bracey
    Nov 21, 2003
  5. Nish
    Replies:
    4
    Views:
    487
    Thomas Stegen
    Oct 8, 2004
Loading...

Share This Page