Instance Inheritance With Run-Time Polymorphism

Programming, for all ages and all languages.
Post Reply
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Instance Inheritance With Run-Time Polymorphism

Post by Kevin McGuire »

I am not too good with names, so I tried to describe what I am curious about with a general topic. To explain what I mean better I would like to give a example of class inheritance and run-time polymorphism.

Code: Select all

class objectA{
  public:
  virtual void DoSomethingFor();
};
class objectB : public objectA {
  public:
  virtual void DoSomethingFor();
};
Alright. I think this is very neat where I can implement more functionality on top of existing functionality, but what I am most curious about is having a chain of inheritance and polymorphism not just for that single instance but for multiple instances.


Its is sort of like you have a instance with layers of functionality brought by polymorphism and additional functionality through inheritance.
class D(class C(class B(class A)))

The problem I have is that it is all contained in a single instance. I would like for this to become dynamic at run-time so that the instance of each inherited class is seperate in contrast to combined.

The only way I can model the wanted behavior, or show it is with:

Code: Select all

class ObjectA
{
	private:
	uint_t data;
	public:
	void DoSomethingFor();
	void DoNothingFor();
};

class ObjectAI
{
	private:
	ObjectA *pb;
	public:
	ObjectAI()
	{
		pb = new ObjectA();
	}
	ObjectAI(ObjectA *_pb)
	{
		pb = _pb;
	}
	virtual void DoSomethingFor()
	{
		pb->DoSomethingFor();
	}
	virtual void DoNothingFor()
	{
		pb->DoNothingFor();
	}
};

class ObjectB
{
	public:
	void DoSomethingFor();
	void DoNothingFor();
}

class ObjectBI : public ObjectAI
{
	private:
	ObjectB *pd;
	ObjectBI()
	{
		pb = new ObjectB();
	}
	ObjectBI(ObjectB *_pb)
	{
		pb = _pb;
	}
	virtual void DoSomethingFor()
	{
		pb->DoSomethingFor();
	}
	virtual void DoNothingFor()
	{
		pb->DoNothingFor();
	}
};

ObjectAI a1(), a2(), a3();
....
ObjectBI b1(&a3), b2(&a1), b1(&a2);
b1.DoSomethingFor();
So that the normal execution flow for the polymorphism, and accessible functionality from inheritance is just like in any normal situation - except the work is delegated out to a seperate instances.

I am wondering is there a easier way to do this than by making two seperate classes for each actual class: one for the actual implementation, and another for the delegation to the actual implementation instance?

<edit>
Let me append on more alternative question that might open the door for ideas. The question is; Is there any way to automatically generate this delegation class during compile time? So that it is not necessary to write a delegation class for every real class that participates in this model.
</code>
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re: Instance Inheritance With Run-Time Polymorphism

Post by Colonel Kernel »

Kevin McGuire wrote:

Code: Select all

ObjectBI b1(&a3), b2(&a1), b1(&a2);
I don't think this would compile, because none of ObjectBI's constructors can take an ObjectAI*, and it doesn't look like ObjectAI is convertible to ObjectB.

Can you give a more concrete example of a situation where this idea would be useful? That might make it more clear exactly what you're trying to do.

Already I suspect the short answer will be to use a different programming language. :wink:
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

Yes. I am looking at CSharp at the moment.

What I really wanted to do was:

[objectA] -> (pointer) -> [instance]
[objectB] -> (pointer) -> [instance]
[objectC] -> (pointer) -> [instance]

So where I can say,

class Device8259 : public Clock, public Device;

Then create a instance, maybe two of them.

Device8259 *d1 = new Device8259();
Device8259 *d2 = new Device8259();

Maybe a abstraction of the device.

class Timer : public Clock;

Timer *t1 = new Timer(d1);

So that 'd1' is a seperate instance like 't1', but 't1' inherits the functionality of 'd1' through inheritance and polymorphism is achieved using any virtual calls.

d1->QueryResources() == t1->QueryResources()

Yet t1 is a seperate instance from d1. The reasoning behind this comes from the ability to do something such as:

Timer *t2 = new Timer(d1);

d1->QueryResource() == t1->QueryResource() == t2->QuerResource() == TRUE;

Simply, because QueryResource is a member function of 'd1' (Device) and both t1 and t2 calls go straight to the instance of 'd1' instead of two seperate instances.

I want the base class(es) to be a seperate instance that can be dynamically changed during run-time.

Not sure If I explained it good enough. The actual usage is still fussy in my head, but it feels like it has a usefulness...
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

It is all syntax sugar, and I was trying to figure out a way to hack it into the language instead of using another language besides C/C++.
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Post by Colonel Kernel »

Kevin McGuire wrote:It is all syntax sugar, and I was trying to figure out a way to hack it into the language instead of using another language besides C/C++.
Yes, I understood that part. Chances are it is very inconvenient, because you're trying to use C++ in a way that it was not intended to be used. It sounds like you want behaviour that is more amenable to a dynamic language like Javascript.

I still don't understand what you're trying to achieve at a higher level though (i.e. -- if you can explain it conceptually instead of at the level of language syntax it would help). It sounds a bit like the Decorator design pattern -- is that the effect you're aiming for?
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

Yes. The decorator pattern is describing what I am wanting to do.

But, I would like to perform this mechanism with out resorting to implementing a decorator class manually for each wrapped class/object. I would actually prefer to not even have a class designated as the decorator but instead have the functionality built in.

I know one more piece of this mechanism that the decorator pattern may cover, but at least does not explicitly state is the ability to change base classes during run-time.

I did a bit of thinking today, but I feel I should refine the idea more since when writing this just a moment ago I realized a loose end to my idea that would preferably need to be sorted out before I try to explain it any more.

You're help is really appreciated and that is including finding a existing description for this mechanism (decorator).
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

Your problem indeed reeks of Decorator at all sides.

If you want to separate handling the object and using/creating the object (such as giving a handle to an unknown object or such) try the Bridge pattern, also known as Handle/Body.

You could also be trying to make a compile-time inheritance tree with something like CRTP which you can apply at runtime in C# by use of reflection. That would allow you to dynamically create classes with functionality in separate classes.

Just the one question remains: why?
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Post by distantvoices »

@Kevin: Well, I'd give the stuff a decorator interface and use the decorator pattern for the stuff you'd like to achieve. Everything else looks like something ugly and flaying.

After all that's what design patterns are good for. You ain't need to reinvent the wheel.

just my 2 cent.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
Post Reply