Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Private interface using reinterpret_cast vs. inheritance (long)

 
 
news_mail_jpa@hotmail.com
Guest
Posts: n/a
 
      08-27-2005
Hi!

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?

Example:

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

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

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

private:
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 {
public:
int usefullStuffToLayer() { return 1; };
Object* getObjectInterface()
{ return reinterpret_cast<Object*>(this) };
private:
friend class Object;

// all Object data items here
Layer* pNext_;
};

// Layer stays the same

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

private:
// 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
 
 
 
Reply

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



Advertisments