Re: C# inheritance broken?



Jake Stevenson's posted an example earlier on this thread.

That's all fine, but it does not get around the cost of *copying* the
object.

I suppose that what I really want here is a sort of "reference"
constructor, something like the "Attach" method in ATL's CComPtr
class. Come to think of it, CComPtr actually "wraps" the interface
being used, but its wrapper is generated automatically by a
preprocessor. Too bad C# doesn't have an automatic wrapper generator
similar to the #import directive in C++!

Tony

On Jan 30, 7:55 am, "Michael D. Ober" <obermd.@.alum.mit.edu.nospam>
wrote:
Tom,

Do you have a an example of how to do this with reflection?

Mike Ober.

"Tom Shelton" <tom_shel...@xxxxxxxxxxxxxxxxxx> wrote in messagenews:XqGdnV7R6qfTjyHYnZ2dnUVZ_oWdnZ2d@xxxxxxxxxxxxxx



On 2007-01-27, Bruce Wood <brucew...@xxxxxxxxxx> wrote:
A generalized method of converting a base class into a derived class
with
the knowledge that the derived class may still require some
initialization
would definitely be useful, even if you had to do it via a constructor
that
takes an object of the base class as it's argument. The missing syntax,
in
vb is

mybase = objBaseClass

In C# I think it would be

base = objBaseClass

In either case, since this is occurring in a constructor in place of the
base.New() or mybase.New statement, the programmer knows he still has to
instantiate and initialize all other private and protected objects in
the
derived class.

Mike Ober.

Agreed. That would be nice. I wouldn't need it often, but it would get
one out of some tricky corners. My only point was that the whole "I
have a base class instance and I want to treat it as though it were a
derived class instance" is completely unsafe and therefore runs
against the grain of what .NET is supposed to be all about. Being able
to copy state, though, would be handy.

Reflection :)

--
Tom Shelton- Hide quoted text -- Show quoted text -

.



Relevant Pages

  • inheritance question
    ... class BaseClass { ... say I have some derived class ... by making SomeVariable a static member of the base class, ... initialization of the variable is needed, and that there may be some ...
    (alt.comp.lang.learn.c-cpp)
  • Re: C++ design question
    ... does not initialize the specific base class instance created as a part ... of creating an instance of this derived class if that was the intent. ...
    (comp.object)
  • Re: C# inheritance broken?
    ... the knowledge that the derived class may still require some initialization ... since this is occurring in a constructor in place of the ... derived class instance" is completely unsafe and therefore runs ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Require base class member to be populated by derived classes.
    ... But it's not very different from any of the other half-way solutions that had already been suggested (nor was it clear from your post that your requirement was that the "population" happen during construction). ... In all cases, the general idea is: have some code in the derived class that initializes the members, and have some code in the base class that verifies that initialization. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C++ design question
    ... >> does not initialize the specific base class instance created as a part ... >> of creating an instance of this derived class if that was the intent. ... >> class instance part if that is the intent. ...
    (comp.object)