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? 3 2957
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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message news:MP******** *************** *@msnews.micros oft.com... 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 MyBarsCollectio n
that does what you describe. Is that what you are suggesting?
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 MyBarsCollectio n 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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 out that
you can inherit using the virtual keyword:
class A
{
}
class B : public *virtual* A
|
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 instance to a
reference type (but not System.String).
The commented out code are lines that when enabled (and any conflicting
lines disabled) stop the exception from being thrown.
Thanks kindly,
Andrew.
|
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
containing a Edit(textbox) and button to test at first. When I change
text in Edit or press button to setText value, I want to monitor the
change event.
But the event code always return 0, why?
|
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 words, I
can do this:
.... pass
....
33
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |