Connecting Tech Pros Worldwide Forums | Help | Site Map

inline+virtual

sunil
Guest
 
Posts: n/a
#1: Aug 26 '08
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

Matthias Buelow
Guest
 
Posts: n/a
#2: Aug 26 '08

re: inline+virtual


sunil wrote:
Quote:
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.)
sunil
Guest
 
Posts: n/a
#3: Aug 26 '08

re: inline+virtual


On Aug 26, 1:56*pm, Matthias Buelow <m...@incubus.dewrote:
Quote:
sunil wrote:
Quote:
*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
Greg Herlihy
Guest
 
Posts: n/a
#4: Aug 26 '08

re: inline+virtual


On Aug 26, 10:03*am, sunil <sunilsreenivas2...@yahoo.comwrote:
Quote:
*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

sunil
Guest
 
Posts: n/a
#5: Aug 27 '08

re: inline+virtual


On Aug 26, 5:37*pm, Greg Herlihy <gre...@mac.comwrote:
Quote:
On Aug 26, 10:03*am, sunil <sunilsreenivas2...@yahoo.comwrote:
>
Quote:
*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)
Pete Becker
Guest
 
Posts: n/a
#6: Aug 27 '08

re: inline+virtual


On 2008-08-27 15:28:42 -0400, sunil <sunilsreenivas2001@yahoo.comsaid:
Quote:
>
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)
But it will. The compiler has to do the right thing. It can generate
the code inline when it's appropriate, and it must provide a definition
for use in a virtual call.

struct Base
{
inline virtual void f();
};

struct Derived : Base
{
inline virtual void f() { ... }
};

void g(Base& bb)
{
Base b;
b.f(); // calls B::f; can be inlined
Derived d;
d.f(); // Calls D::f; can be inlined
bb.f(); // Don't know what it calls; can't be inlined
}

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Closed Thread