473,486 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Change Pointer of a Base Class

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 '06 #1
6 1819
Ma*************@gmail.com 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.

Nov 27 '06 #2
In article <11*********************@j72g2000cwa.googlegroups. com>,
Ma*************@gmail.com 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.
Nov 27 '06 #3
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 '06 #4
In article <11**********************@14g2000cws.googlegroups. com>,
Ma*************@gmail.com 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.
Nov 27 '06 #5
On 27 Nov 2006 03:22:22 -0800 in comp.lang.c++,
Ma*************@gmail.com 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?

Nov 27 '06 #6
David Harmon wrote:
On 27 Nov 2006 03:22:22 -0800 in comp.lang.c++,
Ma*************@gmail.com 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 '06 #7

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

Similar topics

6
5621
by: Abhijit Deshpande | last post by:
Is there any elegant way to acheive following: class Base { public: Base() {} virtual ~Base() {} virtual void Method() { cout << "Base::Method called"; return; } };
9
4956
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
4
3087
by: Gopal-M | last post by:
I have the problem with sizeof operator I also want to implement a function that can return size of an object. My problem is as follows.. I have a Base class, say Base and there are many class...
4
2114
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
8
2167
by: ceo | last post by:
Hi, Following is a program that doesn't give the expected output, not sure what's wrong here. I'm adding the size of derived class to the base class pointer to access the next element in the...
5
2021
by: tthunder | last post by:
Hi @all, Perhaps some of you know my problem, but I had to start a new thread. The old one started to become very very confusing. Here clean code (which compiles well with my BCB 6.0 compiler)....
10
1512
by: Peter Oliphant | last post by:
Is there a way of defining a method in a base class such that derived classes will call their own version, EVEN if the derived instance is referred to by a pointer to the base class? Note that the...
5
3424
by: Scott | last post by:
Hi All, Am I correct in assuming that there is no way to have a base pointer to an object that uses multiple inheritance? For example, class A { /* ... */ }; class B { /* ... */ };
11
3403
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
7
3779
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
0
7123
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,...
0
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5427
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
4863
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
4559
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
3066
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
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.