Change Pointer of a Base Class

Discussion in 'C++' started by MattWilson.6185@gmail.com, Nov 27, 2006.

  1. Guest

    Hi, I'm trying to find out if something is possible, I have a few
    diffrent lists that I add objects to and I would like to be able to
    have a wrapper class that won't affect the internal object, for
    instnace

    class base {
    };

    class a : public base {
    };

    class b : public base {
    };

    class physicsFunctions : public base {
    physicsFunctions(base* _base);
    }

    class displayFuncitons : public base {
    displayFuncitons(base* _base)
    }

    now if I want to create something that is displayable I would like to
    be able to use
    base* a_class = new a();

    displayFunctions* display = new displayFuncitons(a_class);

    furthur more I would like to be able to in the future add functionality
    to it by

    physicsFuncitons* phys = new physicsFuncitons(display);

    I was hopeing that there was some way i could say in the constructor
    don't use a new instance of the base class, use this one that is
    already made, otherwise I am copping allot of code over and over again.

    Thanks!
    , Nov 27, 2006
    #1
    1. Advertising

  2. dasjotre Guest

    wrote:
    > Hi, I'm trying to find out if something is possible, I have a few
    > diffrent lists that I add objects to and I would like to be able to
    > have a wrapper class that won't affect the internal object, for
    > instnace
    >
    > class base {
    > };
    >
    > class a : public base {
    > };
    >
    > class b : public base {
    > };
    >
    > class physicsFunctions : public base {
    > physicsFunctions(base* _base);
    > }
    >
    > class displayFuncitons : public base {
    > displayFuncitons(base* _base)
    > }
    >
    > now if I want to create something that is displayable I would like to
    > be able to use
    > base* a_class = new a();
    >
    > displayFunctions* display = new displayFuncitons(a_class);
    >
    > furthur more I would like to be able to in the future add functionality
    > to it by
    >
    > physicsFuncitons* phys = new physicsFuncitons(display);
    >
    > I was hopeing that there was some way i could say in the constructor
    > don't use a new instance of the base class, use this one that is
    > already made, otherwise I am copping allot of code over and over again.
    >


    You could make a base wrapper class

    class base
    {
    public:
    void foo();
    void bar();
    }

    class base_wrapper
    {
    protected:
    base_wrapper(base * p): base_(p){}

    base * base_;

    public:
    write pass through for base members

    void foo(){ base_->foo(); }
    void bar(){ base_->bar(); }
    }

    And then use base_wrapper as a base for
    physicsFunctions and displayFunctions

    that way you only have to repeat base members
    once.
    dasjotre, Nov 27, 2006
    #2
    1. Advertising

  3. Daniel T. Guest

    In article <>,
    wrote:

    > Hi, I'm trying to find out if something is possible, I have a few
    > diffrent lists that I add objects to and I would like to be able to
    > have a wrapper class that won't affect the internal object, for
    > instnace
    >
    > class base {
    > };
    >
    > class a : public base {
    > };
    >
    > class b : public base {
    > };
    >
    > class physicsFunctions : public base {
    > physicsFunctions(base* _base);
    > }
    >
    > class displayFuncitons : public base {
    > displayFuncitons(base* _base)
    > }
    >
    > now if I want to create something that is displayable I would like to
    > be able to use
    > base* a_class = new a();
    >
    > displayFunctions* display = new displayFuncitons(a_class);
    >
    > furthur more I would like to be able to in the future add functionality
    > to it by
    >
    > physicsFuncitons* phys = new physicsFuncitons(display);
    >
    > I was hopeing that there was some way i could say in the constructor
    > don't use a new instance of the base class, use this one that is
    > already made, otherwise I am copping allot of code over and over again.
    >
    > Thanks!


    You need to break base up into an ABC and a Implementation.

    class AbstractBase {
    public:
    virtual ~AbstractBase() { }
    // pure virtual functions only
    // no data
    };

    class base : public AbstractBase { };

    class a : public AbstractBase {
    base impl;
    };

    class b : public AbstractBase {
    base impl;
    };

    class physicsFunctions : public AbstractBase {
    physicsFunctions( AbstractBase* _base );
    };

    class displayFuncitons: public AbstractBase {
    displayFuncitons( AbstractBase* _base );
    };

    This is called the Decorator pattern.

    --
    To send me email, put "sheltie" in the subject.
    Daniel T., Nov 27, 2006
    #3
  4. Guest

    > You need to break base up into an ABC and a Implementation.
    >
    > class AbstractBase {
    > public:
    > virtual ~AbstractBase() { }
    > // pure virtual functions only
    > // no data
    > };
    >
    > class base : public AbstractBase { };
    >
    > class a : public AbstractBase {
    > base impl;
    > };
    >
    > class b : public AbstractBase {
    > base impl;
    > };
    >
    > class physicsFunctions : public AbstractBase {
    > physicsFunctions( AbstractBase* _base );
    > };
    >
    > class displayFuncitons: public AbstractBase {
    > displayFuncitons( AbstractBase* _base );
    > };
    >
    > This is called the Decorator pattern.


    Thank you for the Reply's!! So the real code is set up as a decorator
    patern right now, however i'm runing into trouble when it comes to
    classes that inherit two items... so the code is more like this
    /*abstract*/
    class Entity {
    virtual render();
    }
    /*abstract*/
    class FixedEntity :public Entity {
    }
    /*abstract*/
    class MobileEntity : public Entity {
    about 10 diffrent functions!
    }
    /*abstract*/
    class Mesh {
    render();
    }

    class MobileMesh : public MobileEntity, public Mesh {
    virtual render() { this->Mesh::render();}
    }

    class FixedMesh : public FixedEntity, publicMesh {
    virtual render() { this->Mesh::render();}
    }

    now I have an active Decorator, and a physics Decorator, the active
    decorator only wants mobile objects (active means they recieve keypress
    handler) and the physics Decorator only wants mesh objects

    class ActiveEntity : public MobileEntity {
    ActiveEntity(MobileEntity* base) : __base(base) {}
    virtual render() {__base->render();}
    virual transform(blblabal) {__base->transform(blblabla);}
    }

    Now the reason I'm having a problem is because ActiveEntity can not
    inherit from Entity, it needs to be considered a MobileEntity, the
    physics problem is even worse because it needs to be considered a mesh
    as well as a mobile or fixed entity, I sorta faked it to work with what
    I belive is a decorator pattern, but theres about 15 functions that are
    being forwarded and there are a bunch of unused fields (if it's a
    physics active mobilemesh then I have 3 versions of the transform
    matries and 2 pointers two the same mesh object though maybe not bad
    it's verry confusing)

    As for casting it is imposible for me to do this

    ActiveEntity* a = new ActiveEntity(new MobileMesh("filename"));
    Mesh* m = dynamic_cast<Mesh*> (a);

    if(m) { //it's a mesh}
    else { //it's a light or a camera }

    because the base is inheriting from just plain MobleEntity

    even worse (the real pitfall) is if I try to do this

    Entity* m = dynamic_cast<Entity*>(a);
    I recieve the wrong entity , I need the bases Entity and sure I could
    write functions to do this but...

    Is there some way to say

    class ActiveEntity: public MobileEntity {
    ActiveEntity(MobileEntity* base) : {this->MobileEntity = base; }
    }

    or should I go back to the design drawing board?

    Thanks =D

    Matt
    , Nov 27, 2006
    #4
  5. Daniel T. Guest

    In article <>,
    wrote:

    > > You need to break base up into an ABC and a Implementation.
    > >
    > > class AbstractBase {
    > > public:
    > > virtual ~AbstractBase() { }
    > > // pure virtual functions only
    > > // no data
    > > };
    > >
    > > class base : public AbstractBase { };
    > >
    > > class a : public AbstractBase {
    > > base impl;
    > > };
    > >
    > > class b : public AbstractBase {
    > > base impl;
    > > };
    > >
    > > class physicsFunctions : public AbstractBase {
    > > physicsFunctions( AbstractBase* _base );
    > > };
    > >
    > > class displayFuncitons: public AbstractBase {
    > > displayFuncitons( AbstractBase* _base );
    > > };
    > >
    > > This is called the Decorator pattern.

    >
    > Thank you for the Reply's!! So the real code is set up as a decorator
    > patern right now, however i'm runing into trouble when it comes to
    > classes that inherit two items... so the code is more like this


    We are getting well outside C++ now, and more into an OO design
    question. As such, you should probably ask in comp.object.

    > /*abstract*/
    > class Entity {
    > virtual render();
    > }
    > /*abstract*/
    > class FixedEntity :public Entity {
    > }
    > /*abstract*/
    > class MobileEntity : public Entity {
    > about 10 diffrent functions!
    > }
    > /*abstract*/
    > class Mesh {
    > render();
    > }
    > class MobileMesh : public MobileEntity, public Mesh {
    > virtual render() { this->Mesh::render();}
    > }
    >
    > class FixedMesh : public FixedEntity, publicMesh {
    > virtual render() { this->Mesh::render();}
    > }
    >
    > now I have an active Decorator, and a physics Decorator, the active
    > decorator only wants mobile objects (active means they recieve keypress
    > handler) and the physics Decorator only wants mesh objects
    >
    > class ActiveEntity : public MobileEntity {
    > ActiveEntity(MobileEntity* base) : __base(base) {}
    > virtual render() {__base->render();}
    > virual transform(blblabal) {__base->transform(blblabla);}
    > }


    MobileEntity and Entity must be pure abstract classes for the above to
    work properly. They should contain no fields. Separate the interface
    from the implementation and have the leaf classes in your hierarchy
    contain the implementation class.

    > Now the reason I'm having a problem is because ActiveEntity can not
    > inherit from Entity, it needs to be considered a MobileEntity, the
    > physics problem is even worse because it needs to be considered a mesh
    > as well as a mobile or fixed entity, I sorta faked it to work with what
    > I belive is a decorator pattern, but theres about 15 functions that are
    > being forwarded and there are a bunch of unused fields (if it's a
    > physics active mobilemesh then I have 3 versions of the transform
    > matries and 2 pointers two the same mesh object though maybe not bad
    > it's verry confusing)
    >
    > As for casting it is imposible for me to do this
    >
    > ActiveEntity* a = new ActiveEntity(new MobileMesh("filename"));
    > Mesh* m = dynamic_cast<Mesh*> (a);
    >
    > if(m) { //it's a mesh}
    > else { //it's a light or a camera }


    It shouldn't be necessary for you to do that. Don't do it.

    > because the base is inheriting from just plain MobleEntity
    > even worse (the real pitfall) is if I try to do this
    >
    > Entity* m = dynamic_cast<Entity*>(a);
    > I recieve the wrong entity , I need the bases Entity and sure I could
    > write functions to do this but...
    >
    > Is there some way to say
    >
    > class ActiveEntity: public MobileEntity {
    > ActiveEntity(MobileEntity* base) : {this->MobileEntity = base; }
    > }
    >
    > or should I go back to the design drawing board?


    You simply need more pure-abstract classes. Classes with no fields and
    only pure-virtual functions.

    --
    To send me email, put "sheltie" in the subject.
    Daniel T., Nov 27, 2006
    #5
  6. David Harmon Guest

    On 27 Nov 2006 03:22:22 -0800 in comp.lang.c++,
    wrote,
    >I was hopeing that there was some way i could say in the constructor
    >don't use a new instance of the base class, use this one that is
    >already made, otherwise I am copping allot of code over and over again.


    A new instance of a derived class always contains a new instance of its
    base class. But that's no reason to copy code. Why are you copying
    code?

    As for the constructor, you use the base copy constructor in the derived
    constructor initializer list, right?
    David Harmon, Nov 27, 2006
    #6
  7. Guest

    David Harmon wrote:
    > On 27 Nov 2006 03:22:22 -0800 in comp.lang.c++,
    > wrote,
    > >I was hopeing that there was some way i could say in the constructor
    > >don't use a new instance of the base class, use this one that is
    > >already made, otherwise I am copping allot of code over and over again.

    >
    > A new instance of a derived class always contains a new instance of its
    > base class. But that's no reason to copy code. Why are you copying
    > code?
    >
    > As for the constructor, you use the base copy constructor in the derived
    > constructor initializer list, right?


    Oooo I see what you're saying, No I wasn't I was storing the base
    instnace and then forwarding the call to the base... This was because
    I needed certain calls to be caughed by the active part of an object if
    it was a physics active object as opposed to a physics object, so....

    /**************************************************************************/
    /* Physics Entity, Serves as a Wraper class for Mobile Entity allowing
    */
    /* Collision detection and apropriate responce control, however it
    still */
    /* inherits from Entity so it can be added to a display queue without
    */
    /* having to grab internal data */
    /**************************************************************************/
    class PhysicsEntity {//: public Entity {
    protected:
    PhysicsEntity();
    public:
    bool Coliding(PhysicsEntity* other);
    private:
    D3DXVECTOR3 __GetCenter();
    float __radius;
    D3DXVECTOR3 __absCenter;
    };



    class MobilePhysicsEntity: public MobileMesh, public PhysicsEntity {
    public:
    MobilePhysicsEntity(MobileEntity* base);
    ~MobilePhysicsEntity() {}
    /* Mobile Entity Members
    *******************************************************************/
    public:
    virtual void WorldRelTran(float x, float y, float z) {
    __base->WorldRelTran(x,y,z); }
    virtual void WorldRelScale(float x, float y, float z)
    {__base->WorldRelScale(x,y,z);}
    public:
    virtual void SetAbsTran(float x, float y, float z)
    {__base->SetAbsTran(x,y,z);}
    virtual void SetAbsRot(float x, float y, float z)
    {__base->SetAbsRot(x,y,z);}
    virtual void SetAbsScale(float x, float y, float z)
    {__base->SetAbsScale(x,y,z);}
    public:
    virtual void RelTran(float x, float y, float z)
    {__base->RelTran(x,y,z);}
    virtual void RelRot(float x, float y, float z) {
    __base->RelRot(x,y,z);}
    virtual D3DXMATRIX GetMatrix() {return __base->GetMatrix();}
    /*******************************************************************************************/
    public:
    virtual void Render(LPDIRECT3DDEVICE9 device)
    {__base->Render(device);}
    virtual LPD3DXMESH GetMesh() { return ((Mesh*)__base)->GetMesh();}
    private:
    MobileEntity* __base;
    /* Called by camera */
    public:
    virtual const D3DXMATRIX __GetTransform() const { return
    __base->__GetTransform();}
    virtual const D3DXMATRIX __GetRotate() const { return
    __base->__GetRotate();};
    virtual const D3DXMATRIX __GetScale() const { return
    __base->__GetScale();};
    /* Physics Addons */
    public:
    };

    this way when if it was an active object originaly it would call the
    active object function, which might modify data then call it's base...
    I see now that this is probably not a good way to do this!! So thank
    you very much I will go back and copy the base data, and treet physics
    objects and active objects seperatly in the code in terms of you have
    to maintain pointers to each individualy and they don't become a super
    class combined!

    Thank you.

    Matt
    , Nov 27, 2006
    #7
    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. tirath
    Replies:
    3
    Views:
    694
    Ivan Vecerina
    Oct 12, 2003
  2. Alf P. Steinbach
    Replies:
    6
    Views:
    530
    John Carson
    Sep 3, 2005
  3. Alberto Luaces
    Replies:
    6
    Views:
    375
    Fraser Ross
    Jun 19, 2006
  4. Hansen
    Replies:
    3
    Views:
    1,080
    rep_movsd
    Apr 24, 2010
  5. , India
    Replies:
    8
    Views:
    931
    gwowen
    Aug 18, 2010
Loading...

Share This Page