Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Private interface using reinterpret_cast vs. inheritance (long)

Thread Tools

Private interface using reinterpret_cast vs. inheritance (long)
Posts: n/a

I'd like to implement a private interface and I have the choices
mentioned in the subject. The implementation using private inheritance
is probably cleaner but it also adds some space and time overhead
compared to the 'cast' -method. The problem with the cast method is
that I don't know whether its C++ standard-compliant and/or portable?


// Inheritance method
class LayerData {
virtual int usefullStuffToLayer() = 0;
virtual Object* getObjectInterface() = 0;

class Layer {
void doSomething(LayerData* p)
int stuff = p->usefullStuffToLayer();
// needs the Object -interface e.g. in callbacks

class Object : private LayerData {
void clientMethod() { pNext_->doSomething(this); };

virtual int usefullStuffToLayer() { return 1; };
virtual Object* getObjectInterface() { return this; };

Layer* pNext_;

Object offers an interface to its clients but wants to hide the
LayerData part. That private interface is passed along when forwarding
calls to the Layer. However, for some reason the Layer also needs the
Object -interface, hence the conversion method. This is okay but it
adds some space (vtbl) and time overhead (v-methods) so I started to
think something like:

class LayerData {
int usefullStuffToLayer() { return 1; };
Object* getObjectInterface()
{ return reinterpret_cast<Object*>(this) };
friend class Object;

// all Object data items here
Layer* pNext_;

// Layer stays the same

class Object {
void clientMethod() { data_.pNext_->doSomething(&data_); };

// this will be the only data-item
LayerData data_;

So, is this portable and/or standard-compliant? Of course I could add a
parent-pointer to the LayerData-object but I wouldn't like to unless
forced to do so (that would effectively reserve the space spared from
the not_needed vtbl-pointer). Object doesn't inherit anything and
nobody is going to inherit from it.

BR, jpa

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
reinterpret_cast<std::size_t>(p) and reinterpret_cast<std::size_t&>() Alex Vinokur C++ 1 02-06-2011 07:48 AM
Interface inheritance vs Implementation inheritance. Daniel Pitts Java 27 02-27-2008 01:37 AM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
Should 'public virtual' always become 'private virtual'? & using private inheritance qazmlp C++ 19 02-04-2004 12:37 AM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM