inline+virtual

S

sunil

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
 
M

Matthias Buelow

sunil said:
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.)
 
S

sunil

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
 
G

Greg Herlihy

 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
 
S

sunil

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)
 

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

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top