473,479 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Private interface using reinterpret_cast vs. inheritance (long)

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

Aug 27 '05 #1
0 1323

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
12796
by: Dave Theese | last post by:
Hello all, The example below demonstrates proper conformance to the C++ standard. However, I'm having a hard time getting my brain around which language rules make this proper... The error...
8
6811
by: Dave | last post by:
Hello all, Suppose that derived inherits privately from base. A base pointer may not be made to point at a derived object in this case. I understand that is exactly what is supposed to happen...
19
2306
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
3
1930
by: Mark A. Gibbs | last post by:
Good day, i'm having a bit of trouble with a base class i'm working on. this is what it boils down to: template <typename T> class foo { protected: foo() { T* p = static_cast<T*>(this); }
21
13779
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
10
1549
by: Bit byte | last post by:
I have a set of C++ classes for which I want to provide a C API - and compile into a C DLL, for use in an application that can only work with a C interface DLL. Any suggestions/pointers on how...
6
1544
by: WXS | last post by:
I know this sounds contrary to the idea of an interface, but read this and see what you think. ----------------------------------------------------------------------------------------- It would be...
6
4395
by: karthikbalaguru | last post by:
Hi, Could someone here tell me some links/pdfs/tutorials to know about the difference between Private Inheritance and Public Inheritance ? I am unable to get info w.r.t it. Thx in advans,...
8
2023
by: puzzlecracker | last post by:
The statement is taken from FAQ . What about non-virtual functions? Can they be overriden? I still don't see a good justification to prefer private inheritance over composition. In fact, I have...
0
6903
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7027
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6726
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5318
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4763
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4468
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
557
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.