Private interface using reinterpret_cast vs. inheritance (long)

Discussion in 'C++' started by news_mail_jpa@hotmail.com, Aug 27, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,197
    Martijn van Steenbergen
    Dec 21, 2003
  2. Suzanne Vogel
    Replies:
    17
    Views:
    846
    Suzanne Vogel
    Jul 7, 2003
  3. qazmlp
    Replies:
    19
    Views:
    828
    Daniel T.
    Feb 4, 2004
  4. karthikbalaguru
    Replies:
    9
    Views:
    1,068
  5. Alex Vinokur
    Replies:
    1
    Views:
    611
Loading...

Share This Page