473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Change base class at runtime?

Perhaps this is a stupid question, but here goes anyway. Given
an instance of an arbitrary class, is it possible to change its base
class at runtime? Here's my problem...

I need to extend class Foo, which I can't change. Foo holds a
reference to a collection of Bar objects and/or objects derived
from Bar (which I can't change). My FooExtended class needs to
keep extra information for each Bar [derivative] in that collection.
If I could change the code that populates/manipulates the Bar
collection, I could arrange for it to hold MyBar objects which
have the extra member variables FooExtended needs. But I can't
change that code either.

At first I figured FooExtended could maintain a parallel collection
that stores info for each of the Bars in its Bar collection, and update
that information by hooking the Bar collection changed event. But
that has become complicated in cases where a Bar instance is
temporarily removed from the collection and then added back at a
later time. The extra info needs to be maintained for the life of every
Bar object, and there could be ALOT of them. I could probably
cache the extra info and associate it with Bar instances, but I'm
wondering if there is a more efficient and elegant approach.

If there is some way I could add member variables (perhaps even
associated accessors) to instances of Bars when they are first seen
in the Bar collection, I *think* my problem would be solved. But
is something like that possible?
Nov 16 '05 #1
3 2942
User N <Us***@invalid.invalid> wrote:
Perhaps this is a stupid question, but here goes anyway. Given
an instance of an arbitrary class, is it possible to change its base
class at runtime?
Nope.
Here's my problem...

I need to extend class Foo, which I can't change. Foo holds a
reference to a collection of Bar objects and/or objects derived
from Bar (which I can't change). My FooExtended class needs to
keep extra information for each Bar [derivative] in that collection.
If I could change the code that populates/manipulates the Bar
collection, I could arrange for it to hold MyBar objects which
have the extra member variables FooExtended needs. But I can't
change that code either.

At first I figured FooExtended could maintain a parallel collection
that stores info for each of the Bars in its Bar collection, and update
that information by hooking the Bar collection changed event. But
that has become complicated in cases where a Bar instance is
temporarily removed from the collection and then added back at a
later time. The extra info needs to be maintained for the life of every
Bar object, and there could be ALOT of them. I could probably
cache the extra info and associate it with Bar instances, but I'm
wondering if there is a more efficient and elegant approach.

If there is some way I could add member variables (perhaps even
associated accessors) to instances of Bars when they are first seen
in the Bar collection, I *think* my problem would be solved. But
is something like that possible?


It doesn't sound like it's feasible - it sounds like Foo just wasn't
designed with this kind of scenario in mind.

Can you override the methods which manipulate the collection, and
whenever you're asked to add a Bar, actually add a MyBar?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MP************************@msnews.microsoft.c om...
User N <Us***@invalid.invalid> wrote:
-snip-
Can you override the methods which manipulate the collection, and
whenever you're asked to add a Bar, actually add a MyBar?


Hmmm. Foo has a public virtual get accessor for its private BarsCollection.
So theoretically MyFoo could override that and return a MyBarsCollection
that does what you describe. Is that what you are suggesting?
Nov 16 '05 #3
User N <Us***@invalid.invalid> wrote:
-snip-
Can you override the methods which manipulate the collection, and
whenever you're asked to add a Bar, actually add a MyBar?


Hmmm. Foo has a public virtual get accessor for its private BarsCollection.
So theoretically MyFoo could override that and return a MyBarsCollection
that does what you describe. Is that what you are suggesting?


Not quite, but it *might* be enough, assuming that you can override
appropriate things in BarsCollection - and assuming that the base class
only ever uses the property, rather than its own member variable...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

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

Similar topics

9
by: Michael Winter | last post by:
Until about 5 minutes ago, I was happy with my knowledge of virtual functions - then I read "Mixing interface and functional inheritance" posted by Kevin L. earlier today. All of a sudden, I found...
2
by: Andrew G. J. Fung | last post by:
Can anyone please explain to me why the code below throws an exception? It seems to occur when deserializing an instance of a subclass, whose base class holds a struct, where said struct holds an...
0
by: zeng.hui.stephen | last post by:
I download the demo http://msdn.microsoft.com/msdnmag/issues/02/10/cuttingedge/. I inherite the demo, and write my code. I want to use Hook to monitor C++ Edit change. I use a C# form...
5
by: Joseph Barillari | last post by:
Hi python-list, I've just started using new-style classes and am a bit confused as to why I can't seem to alter methods with special names (__call__, etc.) of new-style class instances. In other...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...

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.