Change Pointer of a Base Class

M

MattWilson.6185

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!
 
D

dasjotre

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.
 
D

Daniel T.

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.
 
M

MattWilson.6185

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
 
D

Daniel T.

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.
 
D

David Harmon

On 27 Nov 2006 03:22:22 -0800 in comp.lang.c++,
(e-mail address removed) 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?
 
M

MattWilson.6185

David said:
On 27 Nov 2006 03:22:22 -0800 in comp.lang.c++,
(e-mail address removed) wrote,

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top